views:

339

answers:

1

I have an android activity in which I'm using tabs.

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

    TabHost tabHost = getTabHost();
    TabSpec spec;

    spec = tabHost.newTabSpec("controls");
    spec.setIndicator("Control");
    spec.setContent(R.layout.unit_control);
    tabHost.addTab(spec);

    spec = tabHost.newTabSpec("data");
    spec.setIndicator("Data");
    spec.setContent(R.layout.unit_data);
    tabHost.addTab(spec);
  }
}

However when I run the program it crashes with the error: "Could not create tab content because could not find view with id 2130903042". I don't understand what the problem is because R.layout.unit_data refers to a layout file in my resource directory (res/layout/unit_data.xml)

<?xml version="1.0" encoding="utf-8"?>
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
    <TableLayout
        android:stretchColumns="*"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <Spinner android:id="@+id/unit_num"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/choose_unit"/>
    <TableRow android:padding="2dp">
      <TextView
          android:gravity="right"
          android:padding="5dp"
          android:text="@string/Power"/>
      <TextView android:id="@+id/unit_power"
          android:layout_span="3"
          android:gravity="center"
          android:padding="5dp"
          android:background="@android:color/white"
          android:textColor="@android:color/black"
          android:text="AUTO"/>
    </TableRow>
    ...
  </TableLayout>
</ScrollView>

as far as I can tell unit_data.xml is well formed and I've even referenced it successfully in another activity

class UnitData extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.unit_data);
    Toast.makeText(this, "Hi from UnitData.onCreate", 5);
  }
}

which does not give an error and renders the layout just fine.

What's going on? Why can't I reference this layout when creating a tab?

+2  A: 

While Activity.setContentView takes an id of a Layout, TabSpec.setContent takes an id of a View. This means you need to pass an id that looks like R.id.something and not R.layout.something.

To solve your particular problem, give the top level view in your layout a view id:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:id="@+id/unit_data">  <!-- NOTE THE CHANGE -->
  ...
</ScrollView>

and update your source:

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

    TabHost tabHost = getTabHost();
    TabSpec spec;

    spec = tabHost.newTabSpec("controls");
    spec.setIndicator("Control");
    spec.setContent(R.id.unit_control);  // NOTE THE CHANGE
    tabHost.addTab(spec);

    spec = tabHost.newTabSpec("data");
    spec.setIndicator("Data");
    spec.setContent(R.id.unit_data);   // NOTE THE CHANGE
    tabHost.addTab(spec);
  }
}

For more information, see the tab examples in the ApiDemos:

Tim Kryger
Thanks a bunch for the advice. You're right, part of my problem is because I was using a R.layout reference (which looked very similar to an id reference which threw me off). I think also that the id's referenced in this manner must be in the view referenced by @android:id/tabcontent. I read mention of that elsewhere and my own testing seems to support it.
rushinge