2009-06-30 20 views
3

我目前正在嘗試使用Android 1.5 SDK,並且在TabHost上看到了一些示例。 我想要做的是在每個選項卡上使用不同的按鈕來完成其任務。我試過 正在使用onClickListiner()和onClick()。我認爲這是所有開發人員都使用的,但每次按下按鈕時,我都會在LogCat上收到一個空的異常。我也有每個XML佈局,所以我稱之爲標籤爲:tab.add(... setContent(R.id.firstTabLayout))Android 1.5使用TabHost和按鈕進行編程

firstTabLayout = layout for Button and TextView. 

什麼是做一個按鈕/ TextView的正常工作的最佳方法在TabHost下?

回答

0

我不能完全確定您的問題,但是這是我以前怎麼

layout.xml

<TabWidget android:id="@android:id/tabs" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

<FrameLayout android:id="@android:id/tabcontent" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout android:id="@+id/tab1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <TextView android:id="@android:id/text" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 

    </LinearLayout> 
    <LinearLayout android:id="@+id/tab2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <TextView android:id="@android:id/text" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 

    </LinearLayout> 
    <LinearLayout android:id="@+id/tab3" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <TextView android:id="@android:id/text" 
      android:layout_width="fill_parent" 
       android:layout_height="wrap_content" /> 

    </LinearLayout> 
</FrameLayout> 

設置我的分頁活動

Tab.java

public class InitialActivity extends TabActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout); 

     TabHost th = getTabHost(); 
     th.addTab(th.newTabSpec("tab_1").setIndicator("Tab1").setContent(R.id.tab1)); 
     th.addTab(th.newTabSpec("tab_2").setIndicator("Tab2").setContent(R.id.tab2)); 
     th.addTab(th.newTabSpec("tab_3").setIndicator("Tab3").setContent(R.id.tab3)); 
    } 
} 

然後,你可以在選項卡中的任何視圖使用findViewById(),或者如果它們之間有共享的名稱,你可以做findViewById(R.id.tab1).findViewById(R.id.text)