java.lang.ClassCastException无法强制转换为android.app.Fragment

时间:2013-09-29 21:57:22

标签: java android google-maps android-fragments

我有MainActivity类,它有实例化ApplicationBar的方法,所有其他活动都是从MainActivity继承的,所以他们可以使用这个方法。

但我也有一个MapHolder类,必须从FragmentActivity扩展,就像在xml中使用fragment来显示地图一样。问题是如果我从FragmentActivity扩展它我无法显示ApplicationBar选项卡,如果我从MainActivity扩展它而不是我无法显示地图,它会给出这个错误:

LogCat Output:

    09-29 17:52:10.107: E/GooglePlayServicesUtil(3788): Google Play services is invalid. Cannot recover.
09-29 17:52:43.536: E/AndroidRuntime(3832): FATAL EXCEPTION: main
09-29 17:52:43.536: E/AndroidRuntime(3832): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mydomain.myproj/com.mydomain.myproj.MapHolder }: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.os.Looper.loop(Looper.java:137)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at java.lang.reflect.Method.invokeNative(Native Method)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at java.lang.reflect.Method.invoke(Method.java:525)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at dalvik.system.NativeStart.main(Native Method)
09-29 17:52:43.536: E/AndroidRuntime(3832): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.app.Activity.setContentView(Activity.java:1895)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at com.mydomain.myproj.MapHolder .onCreate(MapHolder .java:19)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.app.Activity.performCreate(Activity.java:5133)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-29 17:52:43.536: E/AndroidRuntime(3832):     ... 11 more
09-29 17:52:43.536: E/AndroidRuntime(3832): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.Fragment
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.app.Fragment.instantiate(Fragment.java:585)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.app.Fragment.instantiate(Fragment.java:560)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.app.Activity.onCreateView(Activity.java:4738)
09-29 17:52:43.536: E/AndroidRuntime(3832):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
09-29 17:52:43.536: E/AndroidRuntime(3832):     ... 20 more

2 个答案:

答案 0 :(得分:5)

您正在尝试将SupportMapFragment转换为本机API等级11 android.app.Fragment。那样不行。之一:

  • android:minSdkVersion设置为11或更高,并使用MapFragment代替SupportMapFragment

  • SupportMapFragment投放为android.support.v4.app.Fragment

  

问题是如果我从FragmentActivity扩展它,我就无法显示ApplicationBar Tabs

操作栏已添加到API级别11的Android中。如果您希望在旧版Android上使用操作栏,则需要使用ActionBarSherlockthe AppCompat backport in the Android Support package的后端。

答案 1 :(得分:0)

如果您使用的是com.google.android.gms.maps.SupportMapFragment,则您的活动必须扩展android.support.v4.app.FragmentActivity而不是android.app.Activity