0

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

但我也有一个类 MapHolder,它必须从FragmentActivityxml 扩展,它使用 afragment来显示地图。问题是,如果我从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
4

2 回答 2

5

您正在尝试将 aSupportMapFragment转换为本机 API Level 11 android.app.Fragment。这是行不通的。任何一个:

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

  • 让你SupportMapFragment成为一个android.support.v4.app.Fragment

问题是如果我从 FragmentActivity 扩展它,我无法显示 ApplicationBar 选项卡

操作栏是在 API 级别 11 中添加到 Android 的。如果您希望在旧版本的 Android 上使用操作栏,您将需要使用反向端口,即 Android 支持包中的ActionBarSherlockAppCompat 反向端口

于 2013-09-29T22:01:41.100 回答
0

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

于 2013-09-29T22:03:08.090 回答