tabhost中的片段活动“无法转换为android.support.v4.app.Fragment”

时间:2013-05-28 04:32:47

标签: java android android-fragments android-fragmentactivity

我已经使用TabActivity实现了一个应用程序。现在,我想从TabActvity更改为Fragments,但在我的应用程序中,我需要显示4个选项卡和每个选项卡。以前我创建了TabActivity,现在当我更改为片段时,我有一个错误...这是我的布局:

<?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">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    >
        <TabWidget
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tabsHeader"
            android:layout_alignParentBottom="false"
            android:tabStripEnabled="false"
            android:hapticFeedbackEnabled="true"
            android:background="@drawable/epolicy_header_logo"
        />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:layout_marginTop="40dp"
            android:layout_marginBottom="45dp"
        />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:background="@drawable/epolicy_menu_bar"
            android:tabStripEnabled="false" >
        </TabWidget>

    </RelativeLayout>
</android.support.v4.app.FragmentTabHost>

这是我的活动:

public class EpolicyMainActivity extends FragmentActivity {
private FragmentTabHost tabHost;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    //hide title bar
    BasicDisplaySettings.toggleTaskBar(EpolicyMainActivity.this, false);
    //show status bar
    BasicDisplaySettings.toggleStatusBar(EpolicyMainActivity.this, true);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.epolicy);
    tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
    tabHost.setup(this, getSupportFragmentManager(),android.R.id.tabcontent);
    Resources res = getResources(); 
    Intent intent;  

    tabHost.addTab(tabHost.newTabSpec("Login").setIndicator("Login",
              res.getDrawable(R.drawable.epolicy_menu_xml_home)),
              LoginActivity.class,null);


    tabHost.addTab(tabHost.newTabSpec("NAB").setIndicator("NAB",
            res.getDrawable(R.drawable.epolicy_menu_xml_nab)),
            NABActivity.class,null);



    tabHost.addTab(tabHost.newTabSpec("Contact").setIndicator("Contact",
            res.getDrawable(R.drawable.epolicy_menu_xml_contact)),
            ContactActivity.class,null);


    tabHost.addTab(tabHost.newTabSpec("Agen").setIndicator("Agen",
            res.getDrawable(R.drawable.epolicy_menu_xml_agen)),
            AgenActivity.class,null);



}

我的代码有什么问题吗?我试图导入

import android.support.v4.app.Fragment;

    android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentTabHost;

但是日志猫仍然说cannot be cast to android.support.v4.app.Fragment

我希望有人可以告诉我并告诉我我的错在哪里。

更新

这是我的logcat

05-28 13:39:59.780: E/AndroidRuntime(15156): FATAL EXCEPTION: main
05-28 13:39:59.780: E/AndroidRuntime(15156): java.lang.ClassCastException: id.co.ajsmsig.epolicy.LoginActivity cannot be cast to android.support.v4.app.Fragment
05-28 13:39:59.780: E/AndroidRuntime(15156):    at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
05-28 13:39:59.780: E/AndroidRuntime(15156):    at android.support.v4.app.FragmentTabHost.doTabChanged(FragmentTabHost.java:339)
05-28 13:39:59.780: E/AndroidRuntime(15156):    at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:276)
05-28 13:39:59.780: E/AndroidRuntime(15156):    at android.view.View.dispatchAttachedToWindow(View.java:9831)
05-28 13:39:59.780: E/AndroidRuntime(15156):    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2198)
05-28 13:39:59.780: E/AndroidRuntime(15156):    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206)
05-28 13:39:59.780: E/AndroidRuntime(15156):    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206)
05-28 13:39:59.780: E/AndroidRuntime(15156):    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206)
05-28 13:39:59.780: E/AndroidRuntime(15156):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:953)
05-28 13:39:59.780: E/AndroidRuntime(15156):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2459)
05-28 13:39:59.780: E/AndroidRuntime(15156):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 13:39:59.780: E/AndroidRuntime(15156):    at android.os.Looper.loop(Looper.java:137)
05-28 13:39:59.780: E/AndroidRuntime(15156):    at android.app.ActivityThread.main(ActivityThread.java:4514)
05-28 13:39:59.780: E/AndroidRuntime(15156):    at java.lang.reflect.Method.invokeNative(Native Method)
05-28 13:39:59.780: E/AndroidRuntime(15156):    at java.lang.reflect.Method.invoke(Method.java:511)
05-28 13:39:59.780: E/AndroidRuntime(15156):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-28 13:39:59.780: E/AndroidRuntime(15156):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-28 13:39:59.780: E/AndroidRuntime(15156):    at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案