2014-12-27 2 views
2

Я пытаюсь реализовать простой код, чтобы показать две вкладки на Android, а затем добавить контент, разделяющий на два действия, один из которых показывает примеры с IF, а другой с SWITCH.Вкладки на Android

Я уверен, что создал 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. 

ответ

4

См 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); 
+0

ОК работал ... Tks @Mateus –

Смежные вопросы