Google Maps v2 When Using Fragments on Double-Click

I am trying to set up Google maps in my application inside a fragment using a marker. It works when I click on it for the first time, but when I click on another fragment and then back to Google maps, it crashes. Not sure what the problem is? Any help would be awesome, but just a beginner.

Heres Code:

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"/> 

Java google maps fragment EDIT WORKING CODE BELOW

 public class Fragment_8 extends Fragment{ static final LatLng BottleCapp = new LatLng(51.371986, 0.065593); private GoogleMap map; private static View view; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (view != null) { ViewGroup parent = (ViewGroup) view.getParent(); if (parent != null) parent.removeView(view); } try { view = inflater.inflate(R.layout.fragment_8, container, false); } catch (InflateException e) { /* map is already there, just return view as it is */ } map = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap(); Marker bottleapp = map.addMarker(new MarkerOptions().position(BottleCapp) .title("BottleCapp")); // Move the camera instantly to Bottlecapp with a zoom of 15. map.moveCamera(CameraUpdateFactory.newLatLngZoom(BottleCapp, 15)); // Zoom in, animating the camera. map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); getActivity().getSupportFragmentManager().popBackStack(); return view; } } 

Mistake

  06-15 21:53:46.673: E/AndroidRuntime(8677): FATAL EXCEPTION: main 06-15 21:53:46.673: E/AndroidRuntime(8677): android.view.InflateException: Binary XML file line #2: Error inflating class fragment 06-15 21:53:46.673: E/AndroidRuntime(8677): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 06-15 21:53:46.673: E/AndroidRuntime(8677): at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 06-15 21:53:46.673: E/AndroidRuntime(8677): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 06-15 21:53:46.673: E/AndroidRuntime(8677): at com.bottlecapp.bottlecapp.Fragment_8.onCreateView(Fragment_8.java:29) 06-15 21:53:46.673: E/AndroidRuntime(8677): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460) 06-15 21:53:46.673: E/AndroidRuntime(8677): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911) 06-15 21:53:46.673: E/AndroidRuntime(8677): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) 06-15 21:53:46.673: E/AndroidRuntime(8677): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 06-15 21:53:46.673: E/AndroidRuntime(8677): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) 06-15 21:53:46.673: E/AndroidRuntime(8677): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429) 06-15 21:53:46.673: E/AndroidRuntime(8677): at android.os.Handler.handleCallback(Handler.java:725) 06-15 21:53:46.673: E/AndroidRuntime(8677): at android.os.Handler.dispatchMessage(Handler.java:92) 06-15 21:53:46.673: E/AndroidRuntime(8677): at android.os.Looper.loop(Looper.java:137) 06-15 21:53:46.673: E/AndroidRuntime(8677): at android.app.ActivityThread.main(ActivityThread.java:5041) 06-15 21:53:46.673: E/AndroidRuntime(8677): at java.lang.reflect.Method.invokeNative(Native Method) 06-15 21:53:46.673: E/AndroidRuntime(8677): at java.lang.reflect.Method.invoke(Method.java:511) 06-15 21:53:46.673: E/AndroidRuntime(8677): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 06-15 21:53:46.673: E/AndroidRuntime(8677): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-15 21:53:46.673: E/AndroidRuntime(8677): at dalvik.system.NativeStart.main(Native Method) 06-15 21:53:46.673: E/AndroidRuntime(8677): Caused by: java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id 0x7f05003c, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment 06-15 21:53:46.673: E/AndroidRuntime(8677): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285) 06-15 21:53:46.673: E/AndroidRuntime(8677): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676) 06-15 21:53:46.673: E/AndroidRuntime(8677): ... 18 more 
+8
android android-fragments google-maps google-maps-android-api-2
source share
2 answers

From the code you presented here, there is some error in the objects you are trying to use and you are mixing them. I am talking about the MapFragment object that you use in your xml layout file:

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

if you use MapFragment in your xml, then I donโ€™t understand why you are trying to get the SupportMapFragment object in your activity code:

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

It would be nice to see at what level of API you are targeting your application, but you need to decide what type of support you want to support with API V11 and lower, or you want to support only API V12 and higher. If you want to support the V11 API and see this blog post below, I wrote about integrating the Google API V2 map into your application:

Google Maps V2 API

I want to support only a higher version, and then change the operation code:

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

and modify the manifest file accordingly.

+6
source share

com.google.android.gms.maps.MapFragment based on the Fragment class, which is not in the support package. You need to modify the xml to use com.google.android.gms.maps.SupportMapFragment .

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

Now, for the second failure, the card does not appear to be available. In the V2 library, you need to have the latest Google Play services on your device. Normally the Google Play app will automatically install this for you. If you are using an emulator, Iโ€™m afraid you are out of luck so far, see this answer for using Play Services with an emulator: How do I download Google Play services in an Android emulator?

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

+1
source share

All Articles