Google Maps v2 In fragment cashes when clicked twice

元气小坏坏 提交于 2019-11-29 10:04:00

From the code presented by your here there is some mistake in the objects you are trying to use and you are mixing them up. I'm talking about the MapFragment object that you are using in your xml layout file:

class="com.google.android.gms.maps.MapFragment"

if in your xml your are using MapFragment then I don't see why you are trying to get a SupportMapFragment object in your activity code:

 map = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

It would be nice to see what API level you are targeting your application for, but you need to decide wheater you want to support devices with API V11 and lower or you want to support only API V12 and higher. If you want to support API V11 and lower check out this blog post I wrote on integrating Google API V2 map in your application:

Google Maps API V2

I you seek to support only the higher version then change activity code to this:

map = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

and change your manifest file accordingly.

Flynn81

com.google.android.gms.maps.MapFragment is based on the Fragment class that is not in the support package. You need to change your xml to use com.google.android.gms.maps.SupportMapFragment.

Or not use SupportMapFragment in your Activity. If you are looking for backwards (pre Honeycomb) Fragment support, I recommend changing the class in your xml layout to the SupportMapFragment. If not, consider casting that Fragment to just MapFragment in your activity.

Now, for the second crash, the map doesn't appear to be available. With the V2 map library you need to have the latest Google Play Services running on your device. Usually the Google Play app will automatically install this for you. If you are using an emulator I am afraid you are out of luck at the moment, see this answer to using Play Services with an emulator: How to download Google Play Services in an Android emulator?

To avoid a crash, check out this code sample. Also, for a brief description of map availability, check out the second paragraph in the Class Overview.

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