如何将片段添加到自定义导航抽屉模板?

时间:2015-08-17 04:31:36

标签: android android-fragments material-design

我在android studio中使用来自https://github.com/kanytu/android-material-drawer-template的自定义材质设计导航抽屉模板。 我能够运行导航抽屉,但当我尝试将一个片段添加到抽屉时,整个代码停止并给出错误。我尝试添加片段的方式是。

@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(); 
}

我在Logcat中的错误是

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)

2 个答案:

答案 0 :(得分:2)

Caused by: java.lang.ClassCastException: com.example.geevarughese.nav2.MainActivity@3ddc1486 must implement OnFragmentInteractionListener

看起来ClassCastException是由于缺少您需要实施的OnFragmentInteractionListener而导致的。我不想重复这些努力,所以在这里你可以找到解决方案: https://stackoverflow.com/questions/24777985/how-to-implement-onfragmentinteractionlistener

如果您需要任何帮助,请告诉我。干杯!

答案 1 :(得分:1)

您似乎忘记在OnFragmentInteractionListener上实施MainActivity界面了。当您实施时ClassCastException应该消失。