底部导航片段应用程序不断崩溃

Posted

技术标签:

【中文标题】底部导航片段应用程序不断崩溃【英文标题】:bottom navigation fragment app keeps crashing 【发布时间】:2019-11-25 05:09:00 【问题描述】:

我尝试创建一个底部导航栏,但是当我尝试运行该应用程序时,它一直显示“导航栏已停止”。 我正在使用这个网站的代码:https://www.androidhive.info/2017/12/android-working-with-bottom-navigation/

public class MainActivity extends AppCompatActivity 

    private ActionBar toolbar;

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

        toolbar = getSupportActionBar();

        toolbar.setTitle("Home");
        loadFragment(new HomeFragment());
    

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() 

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) 
            Fragment fragment;
            switch (item.getItemId()) 
                case R.id.navigation_home:
                    toolbar.setTitle("Home");
                    fragment = new HomeFragment();
                    loadFragment(fragment);
                    return true;
                case R.id.navigation_search:
                     toolbar.setTitle("Search");
                    fragment = new SearchFragment();
                    loadFragment(fragment);
                     return true;
                case R.id.navigation_category:
                     toolbar.setTitle("Category");
                    fragment = new CategoryFragment();
                    loadFragment(fragment);
                     return true;
                case R.id.navigation_share:
                     toolbar.setTitle("Share");
                    fragment = new ShareFragment();
                    loadFragment(fragment);
                     return true;
                case R.id.navigation_more_horiz:
                     toolbar.setTitle("More");
                    fragment = new MoreFragment();
                    loadFragment(fragment);
                     return true;
            
            return false;
        
    ;

    private void loadFragment(Fragment fragment) 
        // load fragment
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_container, fragment);
        transaction.addToBackStack(null);
        transaction.commit();

我需要做什么

这是logcat,但我很新不知道该怎么做:

2019-07-16 20:57:32.007 7235-7235/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 7235
java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.myapplication/com.example.myapplication.MainActivity: java.lang.RuntimeException: com.example.myapplication.MainActivity@51ea29f must implement OnFragmentInteractionListener
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.RuntimeException: com.example.myapplication.MainActivity@51ea29f must implement OnFragmentInteractionListener
    at com.example.myapplication.HomeFragment.onAttach(HomeFragment.java:83)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1404)
    at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1195)
    at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1078)
    at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:117)
    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2408)
    at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
    at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
    at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620)
    at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
    at android.app.Activity.performStart(Activity.java:7029)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6494) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

【问题讨论】:

您是否创建了单独的片段类?并显示 logcat 错误 我确实创建了单独的 Fragment 类,我已经添加了 logcat 错误,我相信这是需要的,抱歉我是新手 【参考方案1】:

在你的 MainActivity 中你必须实现接口

public class MainActivity extends AppCompatActivity implement OnFragmentInteractionListener

【讨论】:

当我点击 ALT+enter 时出现错误显示无法解析符号“OnFragmentInteractionListener”,它显示“HomeFragment 中的 OnFragmentInteractionListener”、“...在 CategoryFragment 中”和所有其他片段

以上是关于底部导航片段应用程序不断崩溃的主要内容,如果未能解决你的问题,请参考以下文章

Android 应用程序不断因空指针异常而崩溃

每个底部导航视图菜单项的单独导航图 - Android

通过导航组件在底部导航片段之间传递数据

如何使用 JetpackNavigation 组件从 BottomSheetDialogFragment 导航到另一个 Fragment

重新创建应用时,片段与底部导航视图图标不匹配

底部应用栏在使用片段导航时向上/向下滑动(导航架构组件)