2009-06-30 9 views
3

Je suis actuellement en train d'essayer le SDK Android 1.5 et j'ai vu quelques exemples sur TabHost. Ce que j'essaie de faire est d'utiliser un bouton différent sur chaque onglet pour faire ses tâches. Ce que j'ai essayé utilisait onClickListiner() et onClick(). Je pense que c'est ce que tous les développeurs utilisent, mais je continue à obtenir une exception nulle sur le LogCat chaque fois que le bouton est pressé. Aussi j'ai chaque mise en page XML de sorte que j'appelle l'onglet comme: tab.add (... setContent (R.id.firstTabLayout))Programmation Android 1.5 avec tabHost et boutons

firstTabLayout = layout for Button and TextView. 

Quelle serait la meilleure façon de faire un travail bouton/TextView correctement sous le TabHost?

Répondre

0

Je ne suis pas tout à fait sûr où votre problème, mais voilà comment j'ai mis mes activités avant Tabbed

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)); 
    } 
} 

Ensuite, vous pouvez simplement utiliser findViewById() sur les points de vue dans les onglets, ou s'il y a des noms partagés entre eux, vous pouvez le faire findViewById(R.id.tab1).findViewById(R.id.text)

Questions connexes