片段无法转换为android.support.v4.app.Fragment

时间:2013-11-27 15:53:45

标签: android android-fragments fragment-tab-host

我在实现Fragment tabhost时遇到此错误。

我基本上试图在标签视图中调用一个扩展Fragment的活动。

我也试过扩展FragmentActivity,但它在“onCreateView”方法上给出了错误。 从“onCreateView”方法中删除@override注释后,运行应用程序会导致以下错误。

    11-27 20:54:06.937: E/AndroidRuntime(4255): FATAL EXCEPTION: main
    11-27 20:54:06.937: E/AndroidRuntime(4255): java.lang.ClassCastException: com.variouscal.calculators.FragmentTab1 cannot be cast to android.support.v4.app.Fragment
    11-27 20:54:06.937: E/AndroidRuntime(4255):     at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
    11-27 20:54:06.937: E/AndroidRuntime(4255):     at android.support.v4.app.FragmentTabHost.doTabChanged(FragmentTabHost.java:339)
    11-27 20:54:06.937: E/AndroidRuntime(4255):     at android.support.v4.app.FragmentTabHost.onTabChanged(FragmentTabHost.java:307)
    11-27 20:54:06.937: E/AndroidRuntime(4255):     at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:391)
    11-27 20:54:06.937: E/AndroidRuntime

(4255):     at android.widget.TabHost.setCurrentTab(TabHost.java:376)
11-27 20:54:06.937: E/AndroidRuntime(4255):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
11-27 20:54:06.937: E/AndroidRuntime(4255):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
11-27 20:54:06.937: E/AndroidRuntime(4255):     at android.view.View.performClick(View.java:4084)
11-27 20:54:06.937: E/AndroidRuntime(4255):     at android.view.View$PerformClick.run(View.java:16966)
11-27 20:54:06.937: E/AndroidRuntime(4255):     at android.os.Handler.handleCallback(Handler.java:615)
11-27 20:54:06.937: E/AndroidRuntime(4255):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-27 20:54:06.937: E/AndroidRuntime(4255):     at android.os.Looper.loop(Looper.java:137)
11-27 20:54:06.937: E/AndroidRuntime(4255):     at android.app.ActivityThread.main(ActivityThread.java:4931)
11-27 20:54:06.937: E/AndroidRuntime(4255):     at java.lang.reflect.Method.invokeNative(Native Method)
11-27 20:54:06.937: E/AndroidRuntime(4255):     at java.lang.reflect.Method.invoke(Method.java:511)
11-27 20:54:06.937: E/AndroidRuntime(4255):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
11-27 20:54:06.937: E/AndroidRuntime(4255):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
11-27 20:54:06.937: E/AndroidRuntime(4255):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:5)

问题在于Fragment有两个版本:

  • android.app.Fragment
  • android.support.v4.app.Fragment

请务必导入正确的内容并保持一致。

答案 1 :(得分:1)

如果没有代码,这更像是猜测,但听起来您正试图将android.app.Fragmentandroid.support.v4.app.Fragment一起使用,而这是您无法做到的