我创建了这个小测试用例:
public class MainActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
TabSpec spec;
spec = tabHost.newTabSpec(getString(R.string.service));
spec.setIndicator(getString(R.string.service));
spec.setContent(R.id.first_tab);
tabHost.addTab(spec);
}
}
这是相对布局文档:
<?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>
任何人都知道为什么我在运行时遇到此异常:
java.lang.RuntimeException:无法创建选项卡内容,因为找不到ID为2131034112的视图?
该代码似乎是正确的!
感谢您的任何答复。
最佳答案
您永远不会在代码中加载布局(使用setContentView()),因此将永远不会找到布局内的任何ID。
添加此行:
super.onCreate(savedInstanceState);
setContentView(R.layout.[...]);