与片段的TabActivities

时间:2013-07-29 14:57:42

标签: android android-fragments android-tabhost

简介

我想使用实现使用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

我的频率文件夹

<?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>

1 个答案:

答案 0 :(得分:2)

您是否在xml文件中声明了TabHost而不是

<android.support.v4.app.FragmentTabHost ...  

发生错误是因为编译器尝试查找TabHost但发现了FragmentTabHost。

编辑:

很抱歉,我认为您在xml文件中定义了TabHost,但是您直接在findViewById中定义它。

看看你的进口商品。你导入什么R文件?您正在尝试检索特定支持库R中定义的android.R.tabhost xml文件(可以转换为FragmentTabHost),而不是标准的android.R文件中。

另一个编辑:

如果您确实导入了正确的R文件。我想谈谈我的第一个问题。

可以替换

吗?
  

TabHost

使用

包含在xml文件中
  

android.support.v4.app.FragmentTabHost