Android Google Maps V2 Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.Fragment

匿名 (未验证) 提交于 2019-12-03 09:52:54

问题:

I have this in my class

import android.os.Bundle; import android.support.v4.app.FragmentActivity;  import com.coffeeandcookies.ushuaiamovil.R;  public class LayMapa2 extends FragmentActivity {     @Override     protected void onCreate(Bundle arg0)      {         setContentView(R.layout.lay_mapa2);         super.onCreate(arg0);     } } 

This in my xml

<?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/map"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:name="com.google.android.gms.maps.SupportMapFragment" /> 

This in my manifiest

<uses-sdk         android:minSdkVersion="8"         android:targetSdkVersion="17" />      <uses-feature android:glEsVersion="0x00020000"               android:required="true"/>      <permission         android:name="com.coffeeandcookies.usuahiamovil.permission.MAPS_RECEIVE"         android:protectionLevel="signature" />      <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />     <uses-permission android:name="com.coffeeandcookies.usuahiamovil.permission.MAPS_RECEIVE" />     <uses-permission android:name="android.permission.CALL_PHONE" />     <uses-permission android:name="android.permission.WAKE_LOCK" />     <uses-permission android:name="android.permission.INTERNET" />     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />     <uses-permission android:name="android.permission.READ_PHONE_STATE" />     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />     <uses-permission android:name="android.permission.GET_ACCOUNTS" />   <meta-data             android:name="com.google.android.maps.v2.API_KEY"             android:value="saasadsad" /> 

And I have google-play-services_lib as library proyect in my workspace and the android-support-v4.jar

This is my logcat

07-17 11:46:10.945: E/AndroidRuntime(8241): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.coffeeandcookies.ushuaiamovil/com.coffeeandcookies.vistas.LayMapa2}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.ActivityThread.access$600(ActivityThread.java:140) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.os.Handler.dispatchMessage(Handler.java:99) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.os.Looper.loop(Looper.java:137) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.ActivityThread.main(ActivityThread.java:4895) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at java.lang.reflect.Method.invokeNative(Native Method) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at java.lang.reflect.Method.invoke(Method.java:511) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at dalvik.system.NativeStart.main(Native Method) 07-17 11:46:10.945: E/AndroidRuntime(8241): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:306) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.Activity.setContentView(Activity.java:1912) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at com.coffeeandcookies.vistas.LayMapa2.onCreate(LayMapa2.java:14) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.Activity.performCreate(Activity.java:5163) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061) 07-17 11:46:10.945: E/AndroidRuntime(8241):     ... 11 more 07-17 11:46:10.945: E/AndroidRuntime(8241): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.Fragment 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.Fragment.instantiate(Fragment.java:577) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.Fragment.instantiate(Fragment.java:552) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.Activity.onCreateView(Activity.java:4806) 07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) 07-17 11:46:10.945: E/AndroidRuntime(8241):     ... 20 more 

Thanks!!!

回答1:

Switch super.onCreate first, then setContentView.

This is because super.onCreate calls the framework's Activity.onCreate to allocate the resources needed to inflate any layouts. Hence , must be called before inflating any layouts, xml/programmatic.



回答2:

Change your code like this :

 public class MainActivity extends FragmentActivity  {       private GoogleMap map;      private LatLng ltlng;       @Override      protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.activity_main);        } } 


回答3:

Check the answer of Bhavesh Patadiya, https://stackoverflow.com/a/18227605/2684720

as you are extending FragmentActivity which indicates you are using Support library v4 compatible with lower version of android. Replace MapFragment with SupportMapFragment inside your xml file. SupportMapFragment is the one to use with the Android Support package. MapFragment is for the native API Level 11 version of fragments.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!