更换碎片时应用程序崩溃

时间:2016-09-26 20:30:37

标签: android android-fragments

我开始学习片段,所以我可以在我的应用程序中实现NavigationDrawer。

我配置了NavigationDrawer,然后创建了2个片段:MainFragment,GalleryFragment。

在我的app_bar_main中我创建了一个fragment_container:

app_bar_main:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</FrameLayout>

到目前为止,我可以启动应用程序,它可以正常工作,但NavigationDrawer没有任何效果。

当我想用我的一个片段替换占位符时,应用程序崩溃了:

MainAtivity:

MainFragment fragment = new MainFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

DEBUG:

    09/26 22:25:24: Launching app
Cold swapped changes.
$ adb shell am start -n "com.example.kasimir.schulplaner/com.example.kasimir.schulplaner.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: com.example.kasimir.schulplaner | com.example.kasimir.schulplaner.test
Waiting for application to come online: com.example.kasimir.schulplaner | com.example.kasimir.schulplaner.test
Connecting to com.example.kasimir.schulplaner
I/art: Ignoring second debugger -- accepting and dropping

ANDROIDMONITOR:

FATAL EXCEPTION: main
                                                   Process: com.example.kasimir.schulplaner, PID: 26444
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kasimir.schulplaner/com.example.kasimir.schulplaner.MainActivity}: java.lang.RuntimeException: com.example.kasimir.schulplaner.MainActivity@753f53 must implement OnFragmentInteractionListener
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2456)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2539)
                                                       at android.app.ActivityThread.access$900(ActivityThread.java:159)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:152)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5507)
                                                       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.kasimir.schulplaner.MainActivity@753f53 must implement OnFragmentInteractionListener
                                                       at com.example.kasimir.schulplaner.MainFragment.onAttach(MainFragment.java:84)
                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1043)
                                                       at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:838)
                                                       at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:878)
                                                       at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:719)
                                                       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
                                                       at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                                                       at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:604)
                                                       at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                                                       at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1277)
                                                       at android.app.Activity.performStart(Activity.java:6321)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2539) 
                                                       at android.app.ActivityThread.access$900(ActivityThread.java:159) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:152) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5507) 
                                                       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) 
09-26 22:32:37.725 26444-26444/? D/AppTracker: App Event: crash
09-26 22:32:37.729 3402-4542/? W/ActivityManager:   Force finishing activity com.example.kasimir.schulplaner/.MainActivity
09-26 22:32:37.732 26444-26444/? I/Process: Sending signal. PID: 26444 SIG: 9
09-26 22:32:37.748 3402-26460/? D/DropBoxManagerService: file :: /data/system/dropbox/data_app_crash@2016-09-26-22_32_37_735.txt
09-26 22:32:37.751 3402-4554/? I/ActivityManager: Process com.example.kasimir.schulplaner (pid 26444) has died

1 个答案:

答案 0 :(得分:1)

  

MainActivity @ 753f53必须实现OnFragmentInteractionListener

这是非常不言自明的。您创建项目时生成了OnFragmentInteractionListener。这是用于在片段及其托管活动之间进行通信的常用技术。您有两种选择:1)实现界面或2)删除它。