Android MapFragment exception when clicked twice

My application crashed when I re-open the same MapFragment again twice.

E/AndroidRuntime(27409): Caused by: java.lang.IllegalArgumentException: Binary XML file line #5: Duplicate id 0x7f070039, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment

I solved it by declaring the view in my fragment where I called the map to static. By this way, I only create a new view if it is null, thus avoiding it to recreate the view again.

MapFragment.java

    static View rootView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    	if(rootView==null){
    		rootView = inflater.inflate(R.layout.MapFragment, container, false);
    	}
        return rootView;
    }

MapFragment.xml

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

Posted on July 20, 2014, in Education and tagged , . Bookmark the permalink. 5 Comments.

  1. Great work

  2. Thank you very much

  3. Thanks. It helped me lot… 🙂

  4. TNX FOR SHARING!

  5. U saved me…..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: