我试图实现一个简单的代码,仅在 Android 上显示两个选项卡,然后添加分为两个 Activity 的内容,一个使用 IF 显示示例,另一个使用 SWITCH 显示示例。
我相信我创建的 XML 和 Java 没有任何问题。
运行后,不进入应用程序,提示需要关闭。我一直在使用Android Studio,有什么兼容性问题吗?
我的Java代码:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec1=tabHost.newTabSpec("IF");
spec1.setContent(R.id.IF);
TabSpec spec2=tabHost.newTabSpec("SWITCH");
spec2.setContent(R.id.SWITCH);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
}
}
我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabHost"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"
/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabcontent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/IF"
android:orientation="vertical"
android:paddingTop="60px"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="100px"
android:text="Esta e a aba 01"
android:id="@+id/txt1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/SWITCH"
android:orientation="vertical"
android:paddingTop="60px"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="100px"
android:text="Esta e a aba 2"
android:id="@+id/txt2"
/>
</LinearLayout>
</FrameLayout>
</TabHost>
错误:
12-27 01:24:29.300 30658-30658/com.example.luizhmu.aulas_android_if_switch E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.luizhmu.aulas_android_if_switch, PID: 30658
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.luizhmu.aulas_android_if_switch/com.example.luizhmu.aulas_android_if_switch.MainActivity}: java.lang.IllegalArgumentException: you must specify a way to create the tab indicator.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: you must specify a way to create the tab indicator.
最佳答案
参见Can't find the cause "you must specify a way to create the tab indicator" ,您需要指定一个标签作为选项卡指示器,尝试:
TabHost.TabSpec spec1=tabHost.newTabSpec("IF");
spec1.setContent(R.id.IF);
spec1.setIndicator("your label");
TabHost.TabSpec spec2=tabHost.newTabSpec("SWITCH");
spec2.setContent(R.id.SWITCH);
spec2.setIndicator("your label 2");
tabHost.addTab(spec1);
tabHost.addTab(spec2);
关于android - Android 上的选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27663603/