8

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
user2407147
  • 1,508
  • 2
  • 22
  • 40

2 Answers2

6

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.

Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • I get an error if I change the line to the new one. This error Cannot cast from Fragment to MapFragment. What would I need to change in my manifest? Thanks for the help! I'm targeting 17 but minimum 14 – user2407147 Jun 16 '13 at 09:28
  • Ive looked at the link but they use a FragmentActivity which I can't use, it need to just be a fragment. I have updated code in the question. But now if I click on the Google maps fragment it works then if I click to another fragment then back onto the google maps one, it crashes, any advice? – user2407147 Jun 16 '13 at 09:58
  • again you are mixing things up, if you writing you application for minimum version 14, then you should use MapFragment instead on SupportMapFragment object in your xml file and in your code. In this case you don't need to use FragmentActivity either, use a simple Activity. – Emil Adz Jun 16 '13 at 10:23
  • I need to be as a Fragment due to the way im setting up the app – user2407147 Jun 16 '13 at 10:36
  • sorry I have no experience in placing the google maps fragment inside another fragment. – Emil Adz Jun 16 '13 at 10:39
  • Could you rate my question so more people can see it please :) – user2407147 Jun 16 '13 at 10:43
  • Added the MainActivity to question if that helps? Any ideas? – user2407147 Jun 16 '13 at 12:20
  • Do you know a way of when I click other fragment the google maps one will be destroyed? I think it may be crashing due to it runs then trys to rerun but as its already running so it crashes, could that be and would you know a code that will destroy the fragment when another fragment is clicked? – user2407147 Jun 16 '13 at 12:55
  • You can use the fragmentManager to remove the mapFragment and to added another fragment instead. – Emil Adz Jun 16 '13 at 12:56
  • this will remove the fragment from it's container, But this could be done only if you have added the fragment dynamicly using the fragmentManager. if you defined the mapFramgnet in your xml file then this fragment can't be removed, it's only can be hidden. – Emil Adz Jun 16 '13 at 12:59
  • I don't think that this is your problem. look for a code snippet of a fragment inside of a fragment and see how it's should be done. – Emil Adz Jun 16 '13 at 13:07
  • Ok ill try but I think it will just end up with the same issue – user2407147 Jun 16 '13 at 13:09
  • 3
    Ahh Ive fixed it, The on Create was wrong needed this @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 */ } – user2407147 Jun 16 '13 at 13:22
1

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.

Community
  • 1
  • 1
Flynn81
  • 4,108
  • 29
  • 28
  • Thanks so much, Ive done that but now got new error, i've updated question. – user2407147 Jun 15 '13 at 20:22
  • Oh im using an emulator, ill try a phone thanks, ill get back to you when I try, Thanks so much for all help – user2407147 Jun 15 '13 at 20:36
  • Hey it was great on the phone, thanks so much! Although if I click on the Google maps fragment then to another fragment then back onto it, it crashes, any advice? – user2407147 Jun 15 '13 at 20:41
  • Difficult to say, are you doing anything in onResume? If you want put up the crash log and I'll check it out. – Flynn81 Jun 15 '13 at 20:43
  • Sorry I didn't catch this before, but you are using a fragment in a fragment, which is only supported on Android 4.2 and up, may be the cause of that error. This this question - http://stackoverflow.com/questions/6847460/fragments-within-fragments – Flynn81 Jun 15 '13 at 21:06
  • Im using a 4.2 device nexus 4 – user2407147 Jun 15 '13 at 21:07
  • Which line was line 28 in your Fragment class? Only other thing I can think of is a fragment with the id R.id.map is being added twice to the fragment manager. – Flynn81 Jun 15 '13 at 21:12
  • When you say you click the map fragment, then click on another, and then back, is the order of events like this: new Activity -> new Activity -> back button. Sorry, kind of out of ideas. – Flynn81 Jun 15 '13 at 21:14
  • It goes Load app - Fragment 1 -> Google Maps fragment -> Fragment 1 -> Google Maps fragment Crash with error I write in question – user2407147 Jun 16 '13 at 12:17
  • Added the MainActivity to question if that helps? Any ideas? – user2407147 Jun 16 '13 at 12:21