2014-02-27 33 views
0

我在一個基於標籤視圖無法獲得工作片段選項卡控制器

ToDoActivity.class Android項目以下設置:

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)'? 

但我確實打電話給安裝程序?

回答

0

您還沒有閱讀FragmentTabHost類的文檔,其中明確指出FragmentTabHost是一個特殊的TabHost,它允許使用Fragment對象作爲其標籤內容。因此,您無法將選項卡設置爲活動,並且它無論如何,當你試圖在碎片中進行活動時(它應該是相反的方向),這是沒有意義的。

因此,修改您的代碼以使用片段作爲選項卡內容,或者在活動中使用普通TabHost以繼續將這些活動用作選項卡(此選項已棄用,您應該選擇第一個選項)。在

答源泉:Errors when using a FragmentTabHost inside a Fragment

+0

我已閱讀文檔,活動(ActiveTasks和CompletedTasks)都是Fragment對象。這不是FragmentTabHost打算工作的方式嗎? – patrick

0

原來這是加入意向:

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

一旦我把那些離開並加入了類作爲第二個參數addTab(),它工作得很好。

相關問題