Steve_Chen_LI
2015-08-31 12:02
采纳率: 56.3%
浏览 1.7k

Android——Activity和Fragment 的相关问题

错误内容

     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.esen.aixiaobao/com.esen.aixiaobao.application.news.activity.NewsMainActivity}: android.view.InflateException: Binary XML file line #81: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2344)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
            at android.app.ActivityThread.access$600(ActivityThread.java:163)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1365)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5398)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #81: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:281)
            at android.app.Activity.setContentView(Activity.java:1889)
            at com.esen.aixiaobao.application.news.activity.NewsMainActivity.onCreate(NewsMainActivity.java:26)

Activity部分代码

     public class NewsMainActivity extends FragmentActivity {
    private Fragment[] fragments;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_fragment_news);
        FragmentNews fragment = new FragmentNews();
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_news, fragment).commit();
        fragments = new Fragment[2];
        fragments[0] = getSupportFragmentManager().findFragmentById(R.id.fragment_news);
        fragments[1] = getSupportFragmentManager().findFragmentById(R.id.fragment_focus);
        getSupportFragmentManager().beginTransaction().
                hide(fragments[1]).show(fragments[0]).commit();

    }

Fragment 相关代码

        public class FragmentNews extends Fragment{
    private FragmentActivity activity;
    private MyListView listView;
    private NewsListViewAdapter adapter;
    private List<News> newses;
    //当前页数
    private int pageNow = 0;
    //判断是否加载更多
    private  boolean isLoading =false;

    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.fragment_news,container,false);
        return view;
    }

代码目的,我是想从一个activity(one)跳转到另一个activity(two)上,但是我的
activity(two)是继承FragmentActivity的。 Fragment才是主要显示页面。

                public void onClick(View view) {
        switch (view.getId()) {
                                case R.id.home_family_textview_icon_notify:
                Intent intent = new Intent(getActivity(), NewsMainActivity.class);
                startActivity(intent);
                break;
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • bdmh 2015-08-31 23:06
    已采纳

    文件渲染错误,打开布局文件看看有没有错提示,或者如果你潜入了自己的view,那么这个对象必须有一个不少于两个参数的构造函数才行

    点赞 评论
  • 月盡天明 2015-09-01 03:22

    布局文件里面出错了。找不到这两个fragment

    点赞 评论

相关推荐 更多相似问题