我想用器的使用Tabhost,但我遇到了一些崩潰真的不知道爲什麼。我試圖尋找谷歌開發人員指南,但真的不能找到爲什麼我的應用程序崩潰
我的代碼
public class TabActivities extends FragmentActivity {
private FragmentTabHost mTabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frequencies);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"),
MyLayout1.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"),
MyLayout.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab3"),
MyLayout2.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab4"),
MyLayout3.class, null);
}
}
MyLayout1,2,3只包含佈局的原因
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
}
我已經包含了Android清單中的類。
這裏是我的logcat
07-29 22:50:58.226: E/AndroidRuntime(25877): FATAL EXCEPTION: main
07-29 22:50:58.226: E/AndroidRuntime(25877): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Test.projecttest/com.Test.projecttest.TabActivities}: java.lang.ClassCastException: android.widget.TabHost cannot be cast to android.support.v4.app.FragmentTabHost
07-29 22:50:58.226: E/AndroidRuntime(25877): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
07-29 22:50:58.226: E/AndroidRuntime(25877): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
07-29 22:50:58.226: E/AndroidRuntime(25877): at android.app.ActivityThread.access$700(ActivityThread.java:134)
07-29 22:50:58.226: E/AndroidRuntime(25877): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217)
07-29 22:50:58.226: E/AndroidRuntime(25877): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 22:50:58.226: E/AndroidRuntime(25877): at android.os.Looper.loop(Looper.java:137)
07-29 22:50:58.226: E/AndroidRuntime(25877): at android.app.ActivityThread.main(ActivityThread.java:4856)
07-29 22:50:58.226: E/AndroidRuntime(25877): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 22:50:58.226: E/AndroidRuntime(25877): at java.lang.reflect.Method.invoke(Method.java:511)
07-29 22:50:58.226: E/AndroidRuntime(25877): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
07-29 22:50:58.226: E/AndroidRuntime(25877): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
07-29 22:50:58.226: E/AndroidRuntime(25877): at dalvik.system.NativeStart.main(Native Method)
07-29 22:50:58.226: E/AndroidRuntime(25877): Caused by: java.lang.ClassCastException: android.widget.TabHost cannot be cast to android.support.v4.app.FragmentTabHost
07-29 22:50:58.226: E/AndroidRuntime(25877): at com.Test.projecttest.TabActivities.onCreate(TabActivities.java:22)
07-29 22:50:58.226: E/AndroidRuntime(25877): at android.app.Activity.performCreate(Activity.java:5047)
07-29 22:50:58.226: E/AndroidRuntime(25877): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-29 22:50:58.226: E/AndroidRuntime(25877): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
07-29 22:50:58.226: E/AndroidRuntime(25877): ... 11 more
我frequencies.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="@+android:id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
CAN」您是否使用操作欄選項卡?他們更容易 – scottyseus