Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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.

share|improve this question
    
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 stackoverflow.com/questions/20919048/… – 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
up vote 1 down vote accepted

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()) {

    ....

}
share|improve this answer
    
Do I remove the FragmentManager.remove() on Destroy? – Mikey Feb 28 '15 at 12:05
    
see edited answer – Y.S. Feb 28 '15 at 12:16
    
Would the code be in OnCreateView? – Mikey Feb 28 '15 at 12:18
    
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 – Y.S. Feb 28 '15 at 12:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.