导航抽屉:fragmentTransaction.commit() 使我的应用程序崩溃

Posted

技术标签:

【中文标题】导航抽屉:fragmentTransaction.commit() 使我的应用程序崩溃【英文标题】:Navigation Drawer: fragmentTransaction.commit() is crashing my application 【发布时间】:2018-01-12 00:33:43 【问题描述】:

我正在创建导航抽屉,在我开始添加片段之前它一直有效,它在fragmentTransaction.commit(); 之后崩溃。但我无法理解为什么会这样。谢谢!

这是我的代码:

   import android.support.annotation.Nullable;
   import android.support.v4.app.FragmentTransaction;
   import android.support.v4.widget.DrawerLayout;
   import android.support.v7.app.ActionBarDrawerToggle;
   import android.support.v7.app.AppCompatActivity;
   import android.os.Bundle;
   import android.support.v7.widget.Toolbar;
   import android.util.Log;

  public class MainActivity extends AppCompatActivity 

  Toolbar toolbar;
  DrawerLayout drawerLayout;
  ActionBarDrawerToggle actionBarDrawerToggle;
  FragmentTransaction fragmentTransaction;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar= (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout);
        actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);

        drawerLayout.setDrawerListener(actionBarDrawerToggle);


        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.main_container, new HomeFragment());
        fragmentTransaction.commit();
        getSupportActionBar().setTitle("Home . . .");


    

这是日志:

 FATAL EXCEPTION: main
                                                                          Process: com.example.hakobm.navdrawer, PID: 351
                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.hakobm.navdrawer/com.example.hakobm.navdrawer.MainActivity: java.lang.RuntimeException: com.example.hakobm.navdrawer.MainActivity@f0b255b must implement OnFragmentInteractionListener
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                              at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:148)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                           Caused by: java.lang.RuntimeException: com.example.hakobm.navdrawer.MainActivity@f0b255b must implement OnFragmentInteractionListener
                                                                              at com.example.hakobm.navdrawer.HomeFragment.onAttach(HomeFragment.java:75)
                                                                              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1231)
                                                                              at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1085)
                                                                              at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:976)
                                                                              at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:95)
                                                                              at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
                                                                              at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                                              at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
                                                                              at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                                                                              at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
                                                                              at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                                                                              at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
                                                                              at android.app.Activity.performStart(Activity.java:6268)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                              at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:148) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

【问题讨论】:

在这里发布错误日志。 只要确保你在两个类中使用相同的导入 ***.com/questions/28822229/… 我找到了问题,我的片段 Activity 有问题,我只是创建了一个空的 Activity 并扩展了 Fragment,现在它可以工作了,感谢 ALL 【参考方案1】:

使用以下代码调用您的片段...

  FragmentTransaction fragmenttransaction = getSupportFragmentManager().beginTransaction();
  HomeFragment regcomplainfragment = new HomeFragment();
  fragmenttransaction.replace(R.id.main_container, regcomplainfragment).addToBackStack("tag");
  fragmenttransaction.commit();

并删除下面的行;

  getSupportActionBar().setTitle("Home . . .");

【讨论】:

再次迷恋,我已经添加了日志【参考方案2】:

我觉得这对你有用

    protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context = getApplicationContext();

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawer = (RelativeLayout) findViewById(R.id.drawer);
    mDrawerList = (ListView) findViewById(R.id.drawer_listview);



    if (savedInstanceState == null) 
        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
        tx.replace(R.id.main_container,new HomeFragment());
        tx.commit();
    

    actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close);
    drawerLayout.setDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();


    title = new String[] Here you use menu title
    ;

    icon = new int[] here show menu icon;

    mMenuAdapter = new HomeMenuListAdapter(context, title, icon);
    mDrawerList.setAdapter(mMenuAdapter);
    mMenuAdapter.notifyDataSetChanged();


    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() 
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) 



            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

            switch (position) 

                case 0:

            
            ft.commit();
            mDrawerList.setItemChecked(position, true);
            drawerLayout.closeDrawer(mDrawer);

        
    );


      

【讨论】:

【参考方案3】:

你的 MainActivity 应该实现接口 OnFragmentInteractionListener

【讨论】:

以上是关于导航抽屉:fragmentTransaction.commit() 使我的应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

片段内容未在 BottomNavigationView FragmentTransaction 上加载

多个活动的导航抽屉,无需选择导航抽屉活动

在导航抽屉项目单击时,导航抽屉中的新列表视图

导航抽屉 - 禁用点击抽屉后面的项目

导航抽屉不适用于导航组件

单击导航抽屉引导程序时未显示侧导航栏