如何通过导航抽屉切换碎片(由android提供新的支持库)?

时间:2015-09-13 19:15:11

标签: java android android-fragments

我正在研究android应用程序,我想在Navigation drawer中按照我给出的类别切换片段。当我的活动扩展navigation drawer时,AppcompatActivity正常工作。现在我创建了一个片段HomeFragment extends Fragment,导入的support.v4.Fragment非常好地添加了onCreateonCreateView方法。 现在要应用片段My MainActivity必须扩展FragmentActivity我错了吗?进一步,所以MainActivity extends FragmentActivity,最后我使用

切换片段
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if (fragment == null) {
    fragment = new HomeFragment();
    fm.beginTransaction()
    .add(R.id.fragmentContainer, fragment)
    .commit();
}

我的交换机案例中的这段代码

我的开关案例如下

private void setupNavigationDrawerContent(NavigationView navigationView) {
    navigationView
            .setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    switch (menuItem.getItemId()) {
                    case R.id.home:
                        menuItem.setChecked(true);
                        FragmentManager fm = getSupportFragmentManager();
                        Fragment fragment = fm
                                .findFragmentById(R.id.fragmentContainer);
                        if (fragment == null) {
                            fragment = new HomeFragment();
                            fm.beginTransaction()
                                    .add(R.id.fragmentContainer, fragment)
                                    .commit();
                        }
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.current_event:
                        menuItem.setChecked(true);
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.college_events:
                        menuItem.setChecked(true);

                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.national_events:
                        menuItem.setChecked(true);
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.workshops:
                        menuItem.setChecked(true);
                        FragmentManager fm = getSupportFragmentManager();
                        Fragment fragment = fm
                                .findFragmentById(R.id.list_view_container);
                        if (fragment == null) {
                            fragment = new HomeFragment();
                            fm.beginTransaction()
                                    .add(R.id.list_view_container, fragment)
                                    .commit();
                            drawerLayout.closeDrawer(GravityCompat.START);
                            return true;
                        }
                    case R.id.trainings:
                        menuItem.setChecked(true);

                        Toast.makeText(
                                MainActivity.this,
                                "Launching "
                                        + menuItem.getTitle().toString(),
                                Toast.LENGTH_SHORT).show();
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.companies:
                        menuItem.setChecked(true);
                        Toast.makeText(MainActivity.this,
                                menuItem.getTitle().toString(),
                                Toast.LENGTH_SHORT).show();
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.talents:
                        menuItem.setChecked(true);
                        Toast.makeText(MainActivity.this,
                                menuItem.getTitle().toString(),
                                Toast.LENGTH_SHORT).show();
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.games:
                        menuItem.setChecked(true);
                        Toast.makeText(MainActivity.this,
                                menuItem.getTitle().toString(),
                                Toast.LENGTH_SHORT).show();
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.submission:
                        menuItem.setChecked(true);
                        Toast.makeText(MainActivity.this,
                                menuItem.getTitle().toString(),
                                Toast.LENGTH_SHORT).show();
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    case R.id.about:
                        menuItem.setChecked(true);
                        Toast.makeText(MainActivity.this,
                                menuItem.getTitle().toString(),
                                Toast.LENGTH_SHORT).show();
                        drawerLayout.closeDrawer(GravityCompat.START);
                        return true;
                    }
                    return true;
                }
            });
}

我在活动的XML中使用FrameLayout。每件事情都很好,因为它似乎是问题,当我运行这个应用程序时请问我navigaiondrawer工作正常,但当我试图点击主页类别应用程序正在崩溃。请帮助我

以下是错误日志:

`91404:07:35.05211311131/com.dushyant30suthar.gitseventD/dalvikvmLateenabling CheckJNI
09-14 04:07:35.120    1131-1131/com.dushyant30suthar.gitseventW/dalvikvm:VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
09-14 04:07:35.120    1131-1131/com.dushyant30suthar.gitsevent I/dalvikvmCould not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
09-14 04:07:35.120    1131-1131/com.dushyant30suthar.gitsevent W/dalvikvm VFY: unable to resolve interface method 17391: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
09-14 04:07:35.120    1131-1131/com.dushyant30suthar.gitsevent D/dalvikvm VFY: replacing opcode 0x72 at 0x0002
09-14 04:07:35.120    1131-1131/com.dushyant30suthar.gitsevent I/dalvikvm Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from     method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
09-14 04:07:35.120    1131-1131/com.dushyant30suthar.gitsevent W/dalvikvm VFY: unable to resolve interface method 17395: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
09-14 04:07:35.120    1131-1131/com.dushyant30suthar.gitsevent D/dalvikvm VFY: replacing opcode 0x72 at 0x0002
09-14 04:07:35.136    1131-1131/com.dushyant30suthar.gitsevent I/AppCompatViewInflater﹕ app:theme is now deprecated. Please move to using android:theme instead.
09-14 04:07:35.152    1131-1131/com.dushyant30suthar.gitsevent I/dalvikvm Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
09-14 04:07:35.152    1131-1131/com.dushyant30suthar.gitsevent W/dalvikvm VFY: unable to resolve virtual method 17288: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
09-14 04:07:35.152    1131-1131/com.dushyant30suthar.gitsevent D/dalvikvm VFY: replacing opcode 0x6f at 0x0007
09-14 04:07:35.152    1131-1131/com.dushyant30suthar.gitsevent I/dalvikvm Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
09-14 04:07:35.152    1131-1131/com.dushyant30suthar.gitsevent W/dalvikvm﹕ VFY: unable to resolve virtual method 415: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
09-14 04:07:35.152    1131-1131/com.dushyant30suthar.gitsevent D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
09-14 04:07:35.152    1131-1131/com.dushyant30suthar.gitsevent I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
09-14 04:07:35.152    1131-1131/com.dushyant30suthar.gitsevent W/dalvikvm﹕ VFY: unable to resolve virtual method 437: Landroid/content/res/TypedArray;.getType (I)I
09-14 04:07:35.152    1131-1131/com.dushyant30suthar.gitsevent D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
09-14 04:07:35.152    1131-1131/com.dushyant30suthar.gitsevent I/AppCompatViewInflater﹕ app:theme is now deprecated. Please move to using android:theme instead.
09-14 04:07:35.200    1131-1133/com.dushyant30suthar.gitsevent D/dalvikvm﹕ GC_CONCURRENT freed 202K, 18% free 6189K/7495K, paused 13ms+13ms, total 40ms
09-14 04:07:35.236    1131-1131/com.dushyant30suthar.gitsevent D/dalvikvm﹕ GC_FOR_ALLOC freed 12K, 17% free 6246K/7495K, paused 3ms, total 3ms
09-14 04:07:35.236    1131-1131/com.dushyant30suthar.gitsevent I/dalvikvm-heap﹕ Grow heap (frag case) to 7.440MB for 330412-byte allocation
09-14 04:07:35.252    1131-1131/com.dushyant30suthar.gitsevent D/dalvikvm﹕ GC_FOR_ALLOC freed 1K, 17% free 6567K/7879K, paused 5ms, total 5ms
09-14 04:07:35.264    1131-1131/com.dushyant30suthar.gitsevent D/dalvikvm﹕ GC_FOR_ALLOC freed 1K, 17% free 6569K/7879K, paused 9ms, total 9ms
09-14 04:07:35.268    1131-1131/com.dushyant30suthar.gitsevent I/dalvikvm-heap﹕ Grow heap (frag case) to 8.107MB for 698908-byte allocation
09-14 04:07:35.276    1131-1133/com.dushyant30suthar.gitsevent D/dalvikvm﹕ GC_CONCURRENT freed <1K, 16% free 7251K/8583K, paused 1ms+1ms, total 6ms
09-14 04:07:35.404    1131-1131/com.dushyant30suthar.gitsevent D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so
09-14 04:07:35.404    1131-1131/com.dushyant30suthar.gitsevent D/﹕ HostConnection::get() New Host Connection established 0xb90102d8, tid 1131
09-14 04:07:35.440    1131-1131/com.dushyant30suthar.gitsevent D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so
09-14 04:07:35.440    1131-1131/com.dushyant30suthar.gitsevent D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so
09-14 04:07:35.696    1131-1131/com.dushyant30suthar.gitsevent W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
09-14 04:07:35.732    1131-1131/com.dushyant30suthar.gitsevent D/OpenGLRenderer﹕ Enabling debug mode 0
09-14 04:07:35.896    1131-1131/com.dushyant30suthar.gitsevent D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb90171f0): name, size, mSize = 2, 2304, 2304
09-14 04:07:35.948    1131-1131/com.dushyant30suthar.gitsevent D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb902f068): name, size, mSize = 3, 1024, 3328
09-14 04:07:48.628    1131-1131/com.dushyant30suthar.gitsevent D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb902a918): name, size, mSize = 6, 144, 3472
09-14 04:07:48.652    1131-1131/com.dushyant30suthar.gitsevent D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb9031690): name, size, mSize = 8, 330400, 333872
09-14 04:07:48.664    1131-1131/com.dushyant30suthar.gitsevent D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb9077e88): name, size, mSize = 9, 4, 333876
09-14 04:07:48.800    1131-1131/com.dushyant30suthar.gitsevent D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb9006108): name, size, mSize = 15, 698896, 1032772
09-14 04:07:48.872    1131-1131/com.dushyant30suthar.gitsevent D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb904a070): name, size, mSize = 17, 4096, 1036868
09-14 04:07:48.912    1131-1131/com.dushyant30suthar.gitsevent D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb9022068): name, size, mSize = 18, 4096, 1040964
09-14 04:07:48.916    1131-1131/com.dushyant30suthar.gitsevent D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb9044b10): name, size, mSize = 19, 4096, 1045060
09-14 04:07:48.916    1131-1131/com.dushyant30suthar.gitsevent D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb9020018): name, size, mSize = 20, 4096, 1049156
09-14 04:07:48.920    1131-1131/com.dushyant30suthar.gitsevent D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb901ec20): name, size, mSize = 21, 4096, 1053252
09-14 04:07:48.920    1131-1131/com.dushyant30suthar.gitsevent D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb90364a8): name, size, mSize = 22, 4096, 1057348
09-14 04:07:48.924    1131-1131/com.dushyant30suthar.gitsevent D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb900bcb8): name, size, mSize = 23, 4096, 1061444
09-14 04:07:48.924    1131-1131/com.dushyant30suthar.gitsevent D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb9029c10): name, size, mSize = 24, 4096, 1065540
09-14 04:07:48.924    1131-1131/com.dushyant30suthar.gitsevent D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb900b4d8): name, size, mSize = 25, 4096, 1069636
09-14 04:07:48.928    1131-1131/com.dushyant30suthar.gitsevent D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb9012978): name, size, mSize = 26, 4096, 1073732
09-14 04:07:48.928    1131-1131/com.dushyant30suthar.gitsevent D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb901f218): name, size, mSize = 27, 4096, 1077828
09-14 04:07:50.032    1131-1131/com.dushyant30suthar.gitsevent D/AndroidRuntime﹕ Shutting down VM
09-14 04:07:50.032    1131-1131/com.dushyant30suthar.gitsevent W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa62c8288)
09-14 04:07:50.032    1131-1131/com.dushyant30suthar.gitsevent E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.support.v4.app.SuperNotCalledException: Fragment HomeFragment{534ec534 #0 id=0x7f0d0067} did not call through to super.onCreate()
        at android.support.v4.app.Fragment.performCreate(Fragment.java:1941)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:988)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
09-14 04:07:52.296    1131-1131/? I/Process﹕ Sending signal. PID: 1131 SIG: 9

2 个答案:

答案 0 :(得分:0)

以下是我想为您的查询做出的一些贡献:

  1. 您确定只想从导航抽屉中选择片段.add吗?请记住,有一个.replace功能可用,它会将容器中的当前片段与指定的新片段进行交换。

  2. switch语句中,每次选择导航菜单项时调用getSupportFragmentManager()似乎都会有一些冗余。为什么不在其他任何事情之前调用管理器,并在选择特定菜单项时提供交换片段?

  3. 借助导航抽屉切换片段是一项相当普遍的任务,在网络上以多种方式进行教学。寻求,你会发现!

    祝你好运!

答案 1 :(得分:0)

使用替换代替添加

Fragment fragment = new CrimeFragment();
FragmentManager fm = getFragmentManager();
                        fragmentManager.beginTransaction()
                                .replace(R.id.fragmentContainer, fragment)
                                .commit();