介绍
我想使用实现使用 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 清单中包含了这些类。
这是我的日志猫
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
我的频率.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>