安卓 2.3.3
我只是在尝试如何在不扩展 TabActivity 的情况下实现 TabHost。我得到一个 IllegalArgumentException。请看下面的代码。我不想做任何事情,只是在 TabHost 中添加 3 个选项卡。
来自 Logcat 的异常 :::
05-01 13:41:21.404: E/AndroidRuntime(362): FATAL EXCEPTION: main
05-01 13:41:21.404: E/AndroidRuntime(362): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tabhostdemo/com.example.tabhostdemo.MainActivity}: java.lang.IllegalArgumentException: you must specify a way to create the tab content
05-01 13:41:21.404: E/AndroidRuntime(362): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-01 13:41:21.404: E/AndroidRuntime(362): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-01 13:41:21.404: E/AndroidRuntime(362): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-01 13:41:21.404: E/AndroidRuntime(362): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-01 13:41:21.404: E/AndroidRuntime(362): at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 13:41:21.404: E/AndroidRuntime(362): at android.os.Looper.loop(Looper.java:123)
05-01 13:41:21.404: E/AndroidRuntime(362): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-01 13:41:21.404: E/AndroidRuntime(362): at java.lang.reflect.Method.invokeNative(Native Method)
05-01 13:41:21.404: E/AndroidRuntime(362): at java.lang.reflect.Method.invoke(Method.java:507)
05-01 13:41:21.404: E/AndroidRuntime(362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-01 13:41:21.404: E/AndroidRuntime(362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-01 13:41:21.404: E/AndroidRuntime(362): at dalvik.system.NativeStart.main(Native Method)
05-01 13:41:21.404: E/AndroidRuntime(362): Caused by: java.lang.IllegalArgumentException: you must specify a way to create the tab content
05-01 13:41:21.404: E/AndroidRuntime(362): at android.widget.TabHost.addTab(TabHost.java:202)
05-01 13:41:21.404: E/AndroidRuntime(362): at com.example.tabhostdemo.MainActivity.onCreate(MainActivity.java:22)
05-01 13:41:21.404: E/AndroidRuntime(362): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-01 13:41:21.404: E/AndroidRuntime(362): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-01 13:41:21.404: E/AndroidRuntime(362): ... 11 more
MainActivity.Java:::
public class MainActivity extends Activity {
private TabHost tabhost;
private TabHost.TabSpec tabspec;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabhost = (TabHost) findViewById(R.id.tabhost);
tabspec = tabhost.newTabSpec("SCAN");
tabspec.setIndicator("Scan");
tabhost.addTab(tabspec);
tabspec = tabhost.newTabSpec("CREATE");
tabspec.setIndicator("Create");
tabhost.addTab(tabspec);
tabspec = tabhost.newTabSpec("MORE");
tabspec.setIndicator("More");
tabhost.addTab(tabspec);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml:::
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
谁能告诉我这个错误是什么意思 -you must specify a way to create the tab content
我如何指定它?