2015-02-06 55 views
-1

當我試圖讓我的活動中的選項卡主機顯示錯誤,你忘記打電話public void setup(LocalActivityManager activityGroup)進入我的活動的Tabhost錯誤

public class MainActivity extends FragmentActivity { 
    private Resources res; 
    private TabHost tabHost; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // tabHost = getTabHost(); 
     TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); 

      tabHost.setup(); 

     res = getResources(); 

     Intent intentContact = new Intent().setClass(this, 
       tabOne_Activity.class); 
     TabSpec tabSpecContact = tabHost.newTabSpec("Chat") 
       .setIndicator("", res.getDrawable(R.drawable.ic_launcher)) 
       .setContent(intentContact); 
     tabHost.addTab(tabSpecContact); 

     Intent intentChat = new Intent().setClass(this, tabTwo_Activity.class); 
     TabSpec tabSpecChat = tabHost.newTabSpec("Chat") 
       .setIndicator("", res.getDrawable(R.drawable.ic_launcher)) 
       .setContent(intentChat); 
     tabHost.addTab(tabSpecChat); 

     Intent intentProfile = new Intent().setClass(this, 
       tabThree_Activity.class); 
     TabSpec tabSpecProfile = tabHost.newTabSpec("Chat") 
       .setIndicator("", res.getDrawable(R.drawable.ic_launcher)) 
       .setContent(intentProfile); 
     tabHost.addTab(tabSpecProfile); 

     tabHost.setCurrentTab(2); 


    } 
} 
+0

移動'tabHost.setup(;'最後設置的所有選項卡後 – 2015-02-06 06:46:41

+0

更換Tabhost findviewById本mTabHost =( FragmentTabHost)findViewById(android.R.id.tabhost); – 2015-02-06 06:56:34

+0

@MD然後它顯示空指針異常導致:java.lang.NullPointerException .TabHost.addTab(TabHost.java:236) – 2015-02-06 06:56:48

回答

0

您要使用tabhost.For,你必須擴展TabActivity

OR

寫的onCreate下面的代碼():)

LocalActivityManager mlam = new LocalActivityManager(this, false); 
     mlam.dispatchCreate(savedInstanceState); 
     // tabHost = getTabHost(); 
     //tabHost.setup(); 
     tabHost.setup(mlam); 
+0

它的工作原理:)但當我寫在tab2上的編輯文本中的東西,然後切換選項卡1編輯狀態標籤2的文本是鬆散的plz hepl我如何保持狀態 – 2015-02-06 07:27:26

+0

你必須將文本保存在首選項或sqlite中......它本身不能保持狀態本身神奇。 – kgandroid 2015-02-06 07:45:01