4

I am having a bit if trouble with my Google Maps application. I have three buttons in my application; "Open Google Maps", "Camera" and "Touch". When the application lanuches it goes straight to the Google Maps screen, everything works fine up until this point but when the user is on Google Maps screen and clicks Google Maps button again, the application crashes and I get the following errors in my logcat;

02-28 11:39:25.844: E/AndroidRuntime(2782): FATAL EXCEPTION: main
02-28 11:39:25.844: E/AndroidRuntime(2782): Process: com.GoogleMapsapplication.main, PID: 2782
02-28 11:39:25.844: E/AndroidRuntime(2782): android.view.InflateException: Binary XML file line #33: Error inflating class fragment
02-28 11:39:25.844: E/AndroidRuntime(2782):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at com.augmentedthorpepark.main.FragmentGoogleMap.onCreateView(GoogleMapsFragment.java:50)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at android.app.Fragment.performCreateView(Fragment.java:1700)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at android.app.BackStackRecord.run(BackStackRecord.java:684)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at android.os.Handler.handleCallback(Handler.java:733)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at android.os.Looper.loop(Looper.java:136)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at android.app.ActivityThread.main(ActivityThread.java:5146)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at java.lang.reflect.Method.invokeNative(Native Method)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at java.lang.reflect.Method.invoke(Method.java:515)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at dalvik.system.NativeStart.main(Native Method)
02-28 11:39:25.844: E/AndroidRuntime(2782): Caused by: java.lang.IllegalArgumentException: Binary XML file line #33: Duplicate id 0x7f090035, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
02-28 11:39:25.844: E/AndroidRuntime(2782):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290)
02-28 11:39:25.844: E/AndroidRuntime(2782):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)

it says that I have error in my xml file at line #33 but from my experience and what I found from my search online, this is how I am suppose to use Google Maps;

My xml file with the Google Map code;

<fragment
    android:id="@+id/googlemap"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

Also, I am working with fragments. So I am unable to extend Activity, FragmentActivity etc.

public class GoogleMapsFragment extends Fragment {
 .....
}

Is there any way I can fix this bug, thanks.

Mikey
  • 687
  • 4
  • 17
  • You already have a fragment (extends Fragment) and then in your xml you have a fragment. That's why the **Duplicate id 0x7f090035, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment**. Check this http://stackoverflow.com/questions/20919048/android-android-view-inflateexception-binary-xml-file-line-8-error-inflatin – Raghunandan Feb 28 '15 at 12:00
  • Hi Rghunandan I have already been on that post and tried making mapView but I still get the same error but I will give it another try. – Mikey Feb 28 '15 at 12:03
  • you might be doing it wrong. that post should help – Raghunandan Feb 28 '15 at 12:06

1 Answers1

1

Based on your logcat output, it seems that you are adding the same Fragment twice. This leads to either an IllegalStateException or an IllegalArgumentException.

The solution is to check which Fragment is currently in the foreground first and avoid adding the same one if it is already present:

Fragment f = (Fragment)getFragmentManager().findFragmentByTag("xyz_fragment");
if (f!=null && !f.isVisible() && !f.isAdded()) {

    ....

}
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • Do I remove the FragmentManager.remove() on Destroy? – Mikey Feb 28 '15 at 12:05
  • I get this error on .remove - The method remove() is undefined for the type FragmentManager – Mikey Feb 28 '15 at 12:19
  • @Mikey: apologies, I was thinking of FragmentTransaction.replace(). Please see edited answer – Yash Sampat Feb 28 '15 at 12:56
  • Thanks Zygotelnit, I will try it now. For xyz_fragment, do I need to put reference to my google maps? e.g. Fragment f = (Fragment)getFragmentManager().findFragmentByTag("googlemap"); – Mikey Feb 28 '15 at 14:41
  • That depends on how you have added the `Fragment` ... if you used `FragmentTransaction.add()` method with a tag "googlemap", then yes, you can certainly use `Fragment f = (Fragment)getFragmentManager().findFragmentByTag("googlemap");`. – Yash Sampat Feb 28 '15 at 14:47
  • If I don't need a tag of googlemap, should I leave it as "xyz_fragment" – Mikey Feb 28 '15 at 14:50
  • you need to call `findFragmentById()` whichever tag you have used ... post code of how you have initialized the `Fragment`s – Yash Sampat Feb 28 '15 at 14:52
  • I haven't used any tags, I used have used this .findFragmentById(R.id.googlemap)).getMap() – Mikey Feb 28 '15 at 14:53
  • In that case, try using `(Fragment)getFragmentManager().findFragmentById(R.id.googlemap);` – Yash Sampat Feb 28 '15 at 14:58
  • Thanks. I will try this and let you know – Mikey Feb 28 '15 at 15:08