Android Actionbar替换片段被称为不显示任何内容

时间:2014-03-01 07:51:37

标签: android android-fragments

我完全迷失了,这么简单的事情怎么这么难?我有一个带片段的MainActivity(id:listfragment)。活动开始时显示包含newsitems的列表。我想要做的就是选择一个标签时用另一个标签替换列表。我希望这就够了:

@Override
    public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
        // TODO Auto-generated method stub

        switch(tab.getPosition()) {

        case 0:
            Toast.makeText(this, "Tab News", Toast.LENGTH_SHORT).show();
            // News
            Fragment nfragment = new NewsList();
            fragmentTransaction.replace(R.id.listfragment, nfragment);
            break;

        case 1:
            Toast.makeText(this, "Tab Videos", Toast.LENGTH_SHORT).show();
            // Videos
            Fragment vfragment = new YoutubeList();
            fragmentTransaction.replace(R.id.listfragment, vfragment);
        }

    }

调用新片段(我检查了添加日志)但没有显示布局。请有人请我摆脱苦难;)

我尝试添加.commit()。立即崩溃应用程序。我知道提交是自动调用的(因为他们在de de docs文件中说明)但我不明白为什么它没有显示任何内容。

添加.commit()后记录:

03-01 09:43:00.736: D/dalvikvm(4509): GC_FOR_ALLOC freed 79K, 14% free 10147K/11732K, paused 28ms, total 29ms
03-01 09:43:00.736: I/dalvikvm-heap(4509): Grow heap (frag case) to 12.450MB for 1127536-byte allocation
03-01 09:43:00.756: D/dalvikvm(4509): GC_FOR_ALLOC freed <1K, 13% free 11248K/12836K, paused 19ms, total 19ms
03-01 09:43:00.776: D/AbsListView(4509): Get MotionRecognitionManager
03-01 09:43:00.821: D/AndroidRuntime(4509): Shutting down VM
03-01 09:43:00.821: W/dalvikvm(4509): threadid=1: thread exiting with uncaught exception (group=0x41a03700)
03-01 09:43:00.821: E/AndroidRuntime(4509): FATAL EXCEPTION: main
03-01 09:43:00.821: E/AndroidRuntime(4509): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.switchingbrains.xxx.MainActivity}: java.lang.IllegalStateException: commit already called
03-01 09:43:00.821: E/AndroidRuntime(4509):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at android.app.ActivityThread.access$700(ActivityThread.java:168)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at android.os.Looper.loop(Looper.java:137)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at android.app.ActivityThread.main(ActivityThread.java:5493)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at java.lang.reflect.Method.invokeNative(Native Method)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at java.lang.reflect.Method.invoke(Method.java:525)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at dalvik.system.NativeStart.main(Native Method)
03-01 09:43:00.821: E/AndroidRuntime(4509): Caused by: java.lang.IllegalStateException: commit already called
03-01 09:43:00.821: E/AndroidRuntime(4509):     at android.app.BackStackRecord.commitInternal(BackStackRecord.java:582)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at android.app.BackStackRecord.commit(BackStackRecord.java:574)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:608)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:532)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:518)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at com.switchingbrains.xxx.MainActivity.onCreate(MainActivity.java:29)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at android.app.Activity.performCreate(Activity.java:5372)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
03-01 09:43:00.821: E/AndroidRuntime(4509):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
03-01 09:43:00.821: E/AndroidRuntime(4509):     ... 11 more

1 个答案:

答案 0 :(得分:3)

如果使用fragment标签在xml中定义片段,则无法在运行时替换它。请尝试使用FrameLayout并使用相同的ID。

你可能有这样的事情:

<fragment ....
          android:id="@+id/listfragment"
          ....  />

将其更改为:

<FrameLayout ....
             android:id="@+id/listfragment"
             ....  />