Reputation: 160
I want to be able to use a navigation drawer and change between different fragments depending on selection in the navigation.
I'm using Android Studio and basically what I've done is this:
Then I change some code in the method 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();
}
My program crashes and gives me lots of errors that I don't understand. What am I doing wrong?
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)
Upvotes: 1
Views: 717
Reputation: 41858
When you created the Fragment
with the wizard you left checked a box to create a listener.
So you get this error:
must implement
OnFragmentInteractionListener
If you don't want to use the listener then in 'onAttach' you can remove this block of code:
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
Or in your activity you can implement the listener.
Upvotes: 2