2012-06-07 40 views
-1

我正在關注來自androiddevelopers網站的教程。我複製了代碼並且mTabHost發生了錯誤。我期待看看是否需要導入任何內容,但所有內容都已導入。Android中的TabWidget出錯

代碼

package com.tabTestx; 

import android.app.Activity; 
import android.app.TabActivity; 
import android.os.Bundle; 

public class TabTestXActivity extends TabActivity { 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mTabHost = getTabHost(); 

    mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.id.textview1)); 
    mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2)); 
    mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.textview3)); 

    mTabHost.setCurrentTab(0); 
} 
} 

有人請幫助我

+1

顯示logcat的 – Aerrow

+0

什麼錯誤@Aerrow的mTabHost紅色下劃線。我還沒有運行它。 –

+1

張貼您的XML ... –

回答

1

你忘了申報mTabHost作爲TabHost

TabHost mTabHost = getTabHost(); //declare as TabHost in your code 
^^^^^^^ 
+0

如何將另一個textview添加到相同的tab_test1? –

+0

你不能。你可以在字符串中添加更多的文本'(「tab_test1在這裏添加另一個文本...」)',但不能添加多個文本 –

+0

怎麼樣像4個複選框? –

1

你有Android版本:在XML ID/tabhost?

 <TabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="5dp" 
       android:layout_marginLeft="5dp" 
       android:layout_marginRight="5dp" 
       android:layout_marginTop="5dp" 
       android:background="@drawable/rounded_border"/> 

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="@android:color/white"> 


      </FrameLayout> 
     </LinearLayout> 
    </TabHost>