簡體   English   中英

嘗試開始片段時,應用程序強制關閉

[英]App force closing when trying to start a fragment

我剛剛開始編寫我的第一個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;
    }
};

容器是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)

而是使用commit()嘗試commitNow()。 您的崩潰與以下事實有關:正在進行的待處理片段事務,您嘗試添加一個新事務。 您為什么不將這些片段放在PagerAdapter中,然后一次全部加載? 另一件事,您在主線程上做過多的工作。 在片段中進行大量計算/數據加載時嘗試新線程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM