I am trying to make a tabed interface within the same actvity.
this is my main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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">
<AnalogClock android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<Button android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="A semi-random button"
/>
</FrameLayout>
</LinearLayout>
</TabHost>
dummy.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
and my activity:
public class LActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabs=(TabHost)findViewById(R.id.tabhost);
tabs.setup();
tabs.addTab(tabs.newTabSpec("tag1").setIndicator("1").setContent(R.id.tab1));
tabs.addTab(tabs.newTabSpec("tag2").setIndicator("2").setContent(R.id.tab2));
the above code works, but if you change R.id.tab2 to R.layout.dummy or R.id.ratingBar1, it throws a nullpointerexception.
i intend to display dummy.xml in a tab.
The reason that
R.id.tab2works andR.layout.dummydoesn’t is thatR.id.tab2is part of theActivitycontent view as it is defined in the layout set to it withsetContentView.You can’t use a layout-id where a view-id is expected, and thats why
R.layout.dummydoesn’t work. And you can’t expect it to work when usingR.id.ratingBar1as is not part of theActivitycontent, and how would the activity know where to get that view? Remember you could have several different layouts using an idratingBar1.What you need to do is place your dummy layout within
@android:id/tabcontent. You can do this with just an<include>element. Otherwise, you need to inflate the layout yourself and use the resultingViewas an argument tosetContent.