views:

1411

answers:

1

Hello All,

I am currently trying out the android sdk 1.5 and i have seen a couple of examples on TabHost what i am trying to do is to use a different button on each Tab to do its tasks. What i tried was using onClickListiner() and onClick() i think this is what all the developers use but i keep getting a null exception on the log cat, everytime the button is pressed. Also i have each XML Layout so i call the Tab as : tab.add(...setContent(R.id.firstTabLayout))

firstTabLayout = layout for Button and TextView.

What would be the best way to make a button\TextView work properly under the TabHost

Best Regards

A: 

I'm not entirely sure where your problem is, but this is how I've set up my Tabbed activities before

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

Then, you can just use findViewById() on any views within the tabs, or if there are shared names between them, you can do findViewById(R.id.tab1).findViewById(R.id.text)

Andrew Burgess