gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:20:37 25 4
gpt4 key购买 nike

我有 MainActivity 类,它具有实例化 ApplicationBar 的方法,所有其他 Activity 都是从此 MainActivity 继承的,因此它们可以使用此方法。

但是我还有一个 MapHolder 类,它必须从 FragmentActivity 扩展,因为在 xml 中它使用 fragment 来显示 map 。问题是,如果我从 FragmentActivity 扩展它,我无法显示 ApplicationBar 选项卡,如果我从 MainActivity 扩展它,我就无法显示 map ,并且会出现以下错误:

LogCat 输出:

    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

最佳答案

您正在尝试转换 SupportMapFragment成为 native API 级别 11 android.app.Fragment 。这是行不通的。要么:

  • 设置您的android:minSdkVersion到 11 或更高,并使用 MapFragment而不是SupportMapFragment ,或

  • 转换您的SupportMapFragment成为android.support.v4.app.Fragment

The issue is if I extend it from FragmentActivity I cannot display ApplicationBar Tabs

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

关于java.lang.ClassCastException 无法转换为 android.app.Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19083768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com