2010-07-02 66 views
42

我想創建標籤沒有擴展TabActivity。 (原因是TabActivity無法像處理自定義標題欄那樣)。我有Android:沒有TabActivity的TabHost

public class startTab extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mylayout); 
     Resources res = getResources(); 
     LocalActivityManager mlam = new LocalActivityManager(this, false); 
     TabHost tabHost = (TabHost) findViewById(R.id.tabhost); 
     tabHost.setup(mlam); 
     TabHost.TabSpec spec; 
     Intent intent; 

    intent = new Intent().setClass(this, Show1.class); 
    spec = tabHost.newTabSpec("Items").setIndicator("Items", res.getDrawable(R.drawable.items32_ldpi)).setContent(intent); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, Show2.class); 
    spec = tabHost.newTabSpec("Users").setIndicator("Users",res.getDrawable(R.drawable.user32_ldpi)).setContent(intent); 
    tabHost.addTab(spec); 
} 

}

我得到的是

07-02 07:11:12.715: ERROR/AndroidRuntime(411): 
Caused by: java.lang.IllegalStateException: Activities can't be added until the containing group has been created. 

的查看XML是

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/tabhost" android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 
<LinearLayout android:orientation="vertical" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:paddingTop="5dip"> 
    <TabWidget android:id="@android:id/tabs" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"></TabWidget> 
    <FrameLayout android:id="@android:id/tabcontent" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:paddingTop="5dip"> 
    </FrameLayout> 
</LinearLayout> 
</TabHost> 

我讀的地方,我必須使用LocalActivityManager錯誤,我認爲我錯過了那裏的一些東西。任何人的想法?

謝謝!

+0

本教程可以幫助你http://learnncode.wordpress.com/2013/12/18/how-to-use-tabwidget-with-fragments/ – Prachi 2013-12-23 11:45:37

回答

13

請考慮使用Views作爲標籤的內容。這不僅會導致更少的代碼,更少的堆空間消耗,更少的堆棧空間消耗以及更低的CPU利用率,它還可以幫助您解決這個問題。這裏有twoexamples顯示這種技術。

+1

謝謝。但是,如果我不用意圖工作,我怎樣才能爲每個標籤有不同的菜單? – paradroid666 2010-07-05 06:49:41

+0

@paradroid:根據當前活動選項卡加載不同的菜單。 – CommonsWare 2010-07-05 10:46:23

+0

這就是使用活動的關鍵 - 這是一個更好的功能分離。 – 2011-11-30 17:53:47

1

儘管有設計上的考慮,但以下幾乎不起作用,並且API似乎表明setContent(Intent i)有效。這工作時,活動延伸TabActivity,然而,延長Activity並在exception at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:649)

有趣的是,在logcat的建議我忘了打電話給setup()

mTabHost = (TabHost) findViewById(android.R.id.tabhost); 
mTabHost.setup(); 

Intent tab1Intent = new Intent(this, ActivityOne.class); 
Button tab1View = new Button(this); 
tab1View.setText("Activity 1"); 
    mTabHost.addTab(mTabHost.newTabSpec("tab_1").setIndicator(tab1View).setContent(tab1Intent)); 
+0

這是否工作? – 2011-11-30 18:08:28

+0

沒有爲我工作。 – 2011-12-01 16:35:20

+0

這看起來應該是一個單獨的問題。您可能遇到的問題是您沒有使用TabbedActivity,這意味着您需要使用ActivityManager調用設置,如上所述。這會導致您遇到此問題,除非您正確地將事件分派給ActivityManager。 – shortstuffsushi 2015-09-03 14:33:45

86

調用tabHost.setup然後加入setup()通話效果(mLocalActivityManager );你需要添加這一行。

mlam.dispatchCreate(savedInstanceState); 
tabHost.setup(mlam); 

同樣,你需要添加的onResume,

mlam.dispatchResume(); 

的onPause(),

mlam.dispatchPause(isFinishing()); 
+1

這個工程!有人應該接受這個解決方案! – shihpeng 2011-05-18 09:38:40

+4

但是你能解釋一下爲什麼在添加這些dispatchXXX()方法後工作嗎?非常感謝:) – shihpeng 2011-05-18 09:40:30

+0

偉大的工作對我來說! – 2011-08-29 00:05:41

4
public class ScoreboardActivity extends Activity { 
    LocalActivityManager mlam; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_scoreboard); 
     mlam = new LocalActivityManager(this, false); 
     mlam.dispatchCreate(savedInstanceState); 
     TabHost th = (TabHost) findViewById(android.R.id.tabhost); 
     th.setup(mlam); 
     th.addTab(th.newTabSpec("Numpad").setIndicator("Numpad").setContent(R.id.tab1)); 
     th.addTab(th.newTabSpec("CardCount").setIndicator("CardCount").setContent(R.id.tab2)); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_scoreboard, menu); 
     return true; 


    } 
    @Override 
    protected void onResume(){ 
     super.onResume(); 
     mlam.dispatchResume(); 
    } 

    @Override 
    protected void onPause(){ 
     super.onPause(); 
     mlam.dispatchPause(isFinishing()); 
    } 

} 
+1

'LocalActivityManager'現在已被棄用 – 2013-11-11 21:39:56