Reputation: 1747
I am using a custom material design navigation drawer template from https://github.com/kanytu/android-material-drawer-template in android studio. I am able to run the navigation drawer, but when i am trying to add a fragment to the drawer,the whole code stops and gives error. The way i tried to add fragment is.
@Override
public void onNavigationDrawerItemSelected(int position)
{
// update the main content by replacing fragments
Fragment fragment;
FragmentManager fragmentManager = getSupportFragmentManager(); // For AppCompat use getSupportFragmentManager
switch(position)
{
default: case 0:
fragment = new Fragment1();
break;
case 1: fragment = new Fragment2();
break;
}
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show();
}
My Error In Logcat is
08-17 00:29:59.548 4470-4470/com.example.geevarughese.nav2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.geevarughese.nav2, PID: 4470
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.geevarughese.nav2/com.example.geevarughese.nav2.MainActivity}: java.lang.ClassCastException: com.example.geevarughese.nav2.MainActivity@3ddc1486 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.example.geevarughese.nav2.MainActivity@3ddc1486 must implement OnFragmentInteractionListener
at com.example.geevarughese.nav2.Fragment1.onAttach(Fragment1.java:83)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:907)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
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: 535
Reputation:
Caused by: java.lang.ClassCastException: com.example.geevarughese.nav2.MainActivity@3ddc1486 must implement OnFragmentInteractionListener
It looks like ClassCastException
is caused due to absence of OnFragmentInteractionListener
which you need to implement. I do not want to duplicate the effort so here is where you can find the solution:
https://stackoverflow.com/questions/24777985/how-to-implement-onfragmentinteractionlistener
Just let me know if you need any help. Cheers!
Upvotes: 2
Reputation: 47985
It seems that you forgot to implement the OnFragmentInteractionListener
interface on your MainActivity
. When you implement that your ClassCastException
should be gone.
Upvotes: 1