我希望能够使用导航抽屉,并根据导航中的选择在不同的片段之间进行切换。
我使用的是Android Studio,基本上我所做的就是:
然后我改变了方法 onNavigationDrawerItemSelected 中的一些代码。
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = new BlankFragment();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
我的程序崩溃并给了我很多我不理解的错误。我做错了什么?
4168-4168/com.dev.kim.mytestapplication E/AndroidRuntime﹕ FATAL
EXCEPTION: main
Process: com.dev.kim.mytestapplication, PID: 4168
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dev.kim.mytestapplication/com.dev.kim.mytestapplication.MainActivity}:
java.lang.ClassCastException:
com.dev.kim.mytestapplication.MainActivity@e71881c must implement
OnFragmentInteractionListener
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.ClassCastException: com.dev.kim.mytestapplication.MainActivity@e71881c must implement
OnFragmentInteractionListener
at com.dev.kim.mytestapplication.BlankFragment.onAttach(BlankFragment.java:83)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
at android.app.Activity.performStart(Activity.java:5949)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
答案 0 :(得分:2)
当您使用向导创建Fragment
时,您选中了一个框来创建一个监听器。
所以你得到这个错误:
must implement
OnFragmentInteractionListener
如果您不想使用听众,那么请在“onAttach'你可以删除这段代码:
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
或者在您的活动中,您可以实现监听器。