尝试启动片段时应用程序强制关闭

时间:2018-06-12 11:13:55

标签: java android android-layout android-fragments

我刚刚开始编写我的第一个Android应用程序,但我遇到了一些麻烦。我有一个底部导航栏活动移动到屏幕顶部进行导航,我希望它在按下不同选项卡时在片段之间切换。我的代码设置如下:

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

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()) {
            case R.id.navigation_home:
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.add(R.id.container, new History());
                ft.commit();
                break;
            case R.id.navigation_dashboard:
                FragmentTransaction transaction2 = getSupportFragmentManager().beginTransaction();
                transaction2.replace(R.id.container, new Connect);
                transaction2.commit();
                break;
            case R.id.navigation_notifications:
                FragmentTransaction transaction3 = getSupportFragmentManager().beginTransaction();
                transaction3.replace(R.id.container, new Settings);
                transaction3.commit();
                break;
        }
        return false;
    }
};

Container是我的MainActivity XML中的框架布局。我的问题是代码编译并且似乎工作正常,但是当我单击其中一个选项卡时,它会强制关闭应用程序。我试图找到解决这个问题的方法,但我似乎找不到任何东西,所以如果以前解决了这个问题我会道歉。

编辑:这是logcat

06-12 14:26:07.372 25733-25733/? I/zygote: Not late-enabling -Xcheck:jni (already on)
06-12 14:26:07.443 25733-25733/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
06-12 14:26:08.221 25733-25750/in.co.madhur.chatbubblesdemo D/OpenGLRenderer: HWUI GL Pipeline
06-12 14:26:08.288 25733-25750/in.co.madhur.chatbubblesdemo I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
06-12 14:26:08.288 25733-25750/in.co.madhur.chatbubblesdemo I/OpenGLRenderer: Initialized EGL, version 1.4
06-12 14:26:08.288 25733-25750/in.co.madhur.chatbubblesdemo D/OpenGLRenderer: Swap behavior 1
06-12 14:26:08.289 25733-25750/in.co.madhur.chatbubblesdemo W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
06-12 14:26:08.289 25733-25750/in.co.madhur.chatbubblesdemo D/OpenGLRenderer: Swap behavior 0
06-12 14:26:08.293 25733-25750/in.co.madhur.chatbubblesdemo D/EGL_emulation: eglCreateContext: 0x9b42f4e0: maj 3 min 0 rcv 3
06-12 14:26:08.305 25733-25750/in.co.madhur.chatbubblesdemo D/EGL_emulation: eglMakeCurrent: 0x9b42f4e0: ver 3 0 (tinfo 0x9dd89660)
06-12 14:26:08.499 25733-25750/in.co.madhur.chatbubblesdemo D/EGL_emulation: eglMakeCurrent: 0x9b42f4e0: ver 3 0 (tinfo 0x9dd89660)
06-12 14:26:09.036 25733-25733/in.co.madhur.chatbubblesdemo I/Choreographer: Skipped 34 frames!  The application may be doing too much work on its main thread.
06-12 14:26:10.858 25733-25733/in.co.madhur.chatbubblesdemo V/StudioProfiler: StudioProfilers agent attached.
06-12 14:26:10.958 25733-25796/in.co.madhur.chatbubblesdemo V/StudioProfiler: Acquiring Application for Events
06-12 14:26:11.521 25733-25733/in.co.madhur.chatbubblesdemo V/StudioProfiler: Transformed class: java/net/URL
06-12 14:26:11.522 25733-25733/in.co.madhur.chatbubblesdemo W/zygote: Current dex file has more than one class in it. Calling RetransformClasses on this class might fail if no transformations are applied to it!
06-12 14:26:11.731 25733-25733/in.co.madhur.chatbubblesdemo V/StudioProfiler: Memory control stream started.
06-12 14:26:12.176 25733-25804/in.co.madhur.chatbubblesdemo V/StudioProfiler: Live memory tracking disabled.
06-12 14:26:12.180 25733-25804/in.co.madhur.chatbubblesdemo V/StudioProfiler: Live memory tracking enabled.
    JNIEnv not attached
06-12 14:26:12.339 25733-25804/in.co.madhur.chatbubblesdemo V/StudioProfiler: Loaded classes: 5343
06-12 14:26:13.894 25733-25804/in.co.madhur.chatbubblesdemo V/StudioProfiler: Tracking initialization took: 1713111000ns
06-12 14:27:01.664 25733-25733/in.co.madhur.chatbubblesdemo D/AndroidRuntime: Shutting down VM
06-12 14:27:01.694 25733-25733/in.co.madhur.chatbubblesdemo E/AndroidRuntime: FATAL EXCEPTION: main
    Process: in.co.madhur.chatbubblesdemo, PID: 25733
    java.lang.RuntimeException: in.co.madhur.chatbubblesdemo.Main2Activity@7e4d73d must implement OnFragmentInteractionListener
        at in.co.madhur.chatbubblesdemo.History.onAttach(History.java:83)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1372)
        at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1188)
        at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1071)
        at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:115)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2380)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:703)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        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)

1 个答案:

答案 0 :(得分:0)

而是commit()尝试commitNow()。 您的崩溃与正在进行的待处理片段事务的事实有关,您尝试添加新事务。 为什么不将这些片段放在PagerAdapter中,并一次加载所有片段? 另一件事,你在主线程上做了太多工作。在进行大量计算/数据加载的内部片段时尝试新线程