无法让Fragment选项卡控制器工作

时间:2014-02-27 21:03:16

标签: android

我在Android项目中使用基于标签的视图进行了以下设置

ToDoActivity.class:

private FragmentTabHost mTabHost;
private User user;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_todos);
    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    String username = bundle.getString("username");
    this.user = new User(TodoActivity.this, username);

    mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

    // Tab for 'active' tasks
    TabSpec tasksTab = mTabHost.newTabSpec("Tasks");
    // setting Title and Icon for the Tab
    tasksTab.setIndicator("Tasks", getResources().getDrawable(R.drawable.icon_tasks_tab));
    Intent activeIntent = new Intent(this, ActiveTasks.class);
    tasksTab.setContent(activeIntent);

     // Tab for completed tasks
     TabSpec compeltedTasks = mTabHost.newTabSpec("Completed");        
     compeltedTasks.setIndicator("Completed", getResources().getDrawable(R.drawable.icon_completed_tab));
     Intent completedIntent = new Intent(this, CompletedTasks.class);
     compeltedTasks.setContent(completedIntent);

     // Adding all TabSpec to TabHost
     mTabHost.addTab(tasksTab); 
     mTabHost.addTab(compeltedTasks);
}

activity_todos:XML     

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TabWidget
        android:id="@android:id/tabs"

        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"/>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0"/>

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>
</android.support.v4.app.FragmentTabHost>

每次我运行它并尝试转换到标签视图时,我都会收到以下错误:

02-24 10:03:38.705: E/AndroidRuntime(3791): java.lang.RuntimeException: Unable to start      activity ComponentInfo{edu.gatech.team6.todo/edu.gatech.team6.todo.TodoActivity}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?

但我确实打电话给设置?

2 个答案:

答案 0 :(得分:0)

您还没有阅读FragmentTabHost类的文档,该文档明确指出FragmentTabHost是一个特殊的TabHost,允许使用Fragment对象作为其选项卡内容。所以你不能将标签设置为活动,它会无论如何都没有意义,因为你试图将活动分段(它应该是相反的方式)。

因此,修改代码以使用片段作为选项卡内容,或使用Activity中的常规TabHost继续将这些活动用作选项卡(此选项已弃用,您应该使用第一个选项)。

Errors when using a FragmentTabHost inside a Fragment

回答fount

答案 1 :(得分:0)

原来它正在添加意图:

 Intent activeIntent = new Intent(this, ActiveTasks.class);
 tasksTab.setContent(activeIntent);

一旦我把它们拿走并将Class作为addTab()的第二个参数添加,它就可以了。