Tom
Tom

Reputation: 3637

Android TabHost strange error

Since TabHost itself is not deprecated, and I just need a very simple solution to switch between two controls (maybe 3) I decided TabHost was perfect for my needs. However I get a very mysterious error....

This is the code I have

myTabHost = (TabHost) findViewById(android.R.id.tabhost);
myTabHost.setup();
TabHost.TabSpec tmpSpec = null;
TabContentFactory tmpContentFactory = null;
tmpSpec = myTabHost.newTabSpec("main_param_time");
tmpSpec.setIndicator("sometext");

// error happens here here
tmpSpec.setContent(R.id.charts_stickchart);

// rest of code here. e.g. addTab etc.

The error I get is

java.lang.RuntimeException: Could not create tab content because could not find view with id 2131427359

My XML file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
<TabHost
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@android:id/tabhost"
 >
        <LinearLayout
                android:id="@+id/LinearLayout01"
                android:orientation="vertical"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent">    
                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent">
                </TabWidget>
                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_height="fill_parent"
                     android:layout_width="fill_parent"
                 >
                </FrameLayout>
        </LinearLayout>
</TabHost>
        <cn.limc.androidcharts.view.StickChart
            android:id="@+id/charts_stickchart"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:visibility="gone"
            />       
        <cn.limc.androidcharts.view.SpiderWebChart
            android:id="@+id/charts_spiderwebchart"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:visibility="gone"
            />
</LinearLayout>

Upvotes: 0

Views: 259

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007533

Move your two charts to be children of the <FrameLayout>, as in this sample app:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/tabhost"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TabWidget android:id="@android:id/tabs"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
    />
    <FrameLayout android:id="@android:id/tabcontent"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <AnalogClock android:id="@+id/tab1"
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
      />
      <Button android:id="@+id/tab2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="A semi-random button"
      />
    </FrameLayout>
  </LinearLayout>
</TabHost>

Upvotes: 1

Related Questions