Android TabActivity and setContent(int idView) ---> RuntimeException
I have created this little testcase:
public class MainActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
TabSpec spec;
开发者_Python百科 spec = tabHost.newTabSpec(getString(R.string.service));
spec.setIndicator(getString(R.string.service));
spec.setContent(R.id.first_tab);
tabHost.addTab(spec);
}
}
and this is the relative layout document:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/first_tab">
<ToggleButton android:id="@+id/serviceButton"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textOn="@string/service_on" android:textOff="@string/service_off">
</ToggleButton>
</RelativeLayout>
Anyone know why i get this exception at runtime: java.lang.RuntimeException: Could not create tab content because could not find view with id 2131034112 ?
The code seems to be right!
Thanks you for any reply.
You never load your layout in your code (with setContentView()), so any IDs inside your layout will never be found.
Add this line :
super.onCreate(savedInstanceState);
setContentView(R.layout.[...]);
Your layout doesn't have a TabHost element in it. The runtime is looking specifically for a TabHost View with id string "@android:id/tabhost" and it can't find it. I've tried to make a ListActivity without a ListView and come across the same sort of error. See this for an example of a tabbed XML layout.
精彩评论