Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps v2 In fragment cashes when clicked twice

I'm trying to set up Google maps in my app inside a fragment with a marker. It works when I click on it the first time but when I click on another fragment then back to the Google maps one it crashes. Not sure what the issue is? Any help would be awesome, only 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;

    }
  }

Error

 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
like image 508
user2407147 Avatar asked Jun 15 '13 20:06

user2407147


2 Answers

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.

like image 109
Emil Adz Avatar answered Oct 25 '22 02:10

Emil Adz


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.

like image 25
Flynn81 Avatar answered Oct 25 '22 02:10

Flynn81