Use GoogleMaps crash app when replacing with Fragmentmanager

Source: Internet
Author: User

I want to use the following code to load the fragment of the item has been selected in Navigationdrawer, however mapfragment contains it inside the Google Map fragment and it crashes after trying to open it for the second time.

Here is the code I used:

@Override    public void onNavigationDrawerItemSelected(int position) {        // update the main content by replacing fragments        FragmentManager fragmentManager = getSupportFragmentManager();        if (position == 0) {            fragmentManager.beginTransaction()                    .replace(R.id.container, MapFragment.newInstance("a", "b"))                    .commit();        }        else {            fragmentManager.beginTransaction()                    .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))                    .commit();        }    }

Here is the log:

03-01 14:18:25.870 27175-27175/com.br.appname e/:device driver API match Device driver API version:23 User space API version:2303-01 14:18:25.870 27175-27175/com.br.appname e/:mali:revision=linux-r3p2-01rel3 BUILD_DATE=Wed Oct 30 0 9:36:10 KST 201303-01 14:19:12.655 27175-27175/com.br.appname e/androidruntime:fatal exception:main android.view.Inf Lateexception:binary XML file line #13: Error inflating class fragment at Android.view.LayoutInflater.createVi            Ewfromtag (layoutinflater.java:719) at Android.view.LayoutInflater.rInflate (layoutinflater.java:761) At Android.view.LayoutInflater.rInflate (layoutinflater.java:769) at Android.view.LayoutInflater.inflate (Layouti nflater.java:498) at Android.view.LayoutInflater.inflate (layoutinflater.java:398) at Com.br.appname. Fragments. Mapfragment.oncreateview (mapfragment.java:74) at Android.support.v4.app.Fragment.performCreateView (Fragment.jav       A:1500)     At Android.support.v4.app.FragmentManagerImpl.moveToState (fragmentmanager.java:927) at android.support.v4. App. Fragmentmanagerimpl.movetostate (fragmentmanager.java:1104) at Android.support.v4.app.BackStackRecord.run (Backst             ackrecord.java:682) at Android.support.v4.app.FragmentManagerImpl.execPendingActions (fragmentmanager.java:1467) At Android.support.v4.app.fragmentmanagerimpl$1.run (fragmentmanager.java:440) at Android.os.Handler . Handlecallback (handler.java:730) at Android.os.Handler.dispatchMessage (handler.java:92) at Android. Os. Looper.loop (looper.java:137) at Android.app.ActivityThread.main (activitythread.java:5419) at java.la  Ng.reflect.Method.invokeNative (Native Method) at Java.lang.reflect.Method.invoke (method.java:525) at Com.android.internal.os.zygoteinit$methodandargscaller.run (zygoteinit.java:1209) at Com.android.internal.os.Zy Goteinit.main (Zygoteinit.java:1025) at Dalvik.system.NativeStart.main (Native Method) caused BY:JAVA.LANG.ILLEGALARGUMENTEXC Eption:binary XML file line #13: Duplicate ID 0x7f070055, tag null, or the parent ID 0x7f070054 with another fragment for COM . Google.android.gms.maps.MapFragment at Android.app.Activity.onCreateView (activity.java:4971) at and Roid.view.LayoutInflater.createViewFromTag (layoutinflater.java:695)              at android.view.LayoutInflater.rInflate (layoutinflater.java:761)              at Android.view.LayoutInflater.rInflate ( layoutinflater.java:769)             at Android.view.LayoutInflater.inflate (layoutinflater.java:498)              at android.view.LayoutInflater.inflate (layoutinflater.java:398)      &Nbsp;       at Com.br.appname.fragments.MapFragment.onCreateView ( mapfragment.java:74)             at Android.support.v4.app.Fragment.performCreateView (fragment.java:1500)              at Android.support.v4.app.FragmentManagerImpl.moveToState ( fragmentmanager.java:927)             at Android.support.v4.app.FragmentManagerImpl.moveToState (fragmentmanager.java:1104)              at Android.support.v4.app.BackStackRecord.run ( backstackrecord.java:682)             at Android.support.v4.app.FragmentManagerImpl.execPendingActions (fragmentmanager.java:1467)              at android.support.v4.app.fragmentmanagerimpl$1. Run (fragmentmanager.java:440)             at Android.os.Handler.handleCallback (handler.java:730)              at android.os.Handler.dispatchMessage (handler.java:92)              at Android.os.Looper.loop (looper.java:137)              at Android.app.ActivityThread.main (activitythread.java:5419)              at java.lang.reflect.Method.invokeNative ( Native Method)             at Java.lang.reflect.Method.invoke (method.java:525)              at Com.android.internal.os.zygoteinit$methodandargscaller.run (zygoteinit.java:1209)             &Nbsp;at Com.android.internal.os.ZygoteInit.main (zygoteinit.java:1025)              at Dalvik.system.NativeStart.main (Native Method)

This happens only with mapfragment. Placeholderfragment is just normal.

Update

Fragment_map.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context = "Com.br.remotepark.fragments.MapFragment" > <com.sothree.slidinguppanel.slidinguppanellayout android:id= "@ +id/sliding_layout "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:            gravity= "Bottom" > <fragment android:id= "@+id/map" android:layout_width= "Match_parent"        android:layout_height= "Match_parent" android:name= "Com.google.android.gms.maps.MapFragment"/>            <linearlayout android:layout_width= "match_parent" android:layout_height= "Match_parent" Android:background= "#eee" android:orientation= "vertical" android:clickable= "true" an Droid:focusable= "false" > <liNearlayout android:layout_width= "match_parent" android:layout_height= "68DP" a                    ndroid:orientation= "Horizontal" > <textview android:id= "@+id/name" Android:layout_width= "0DP" android:layout_height= "Match_parent" Android:layout_we                    ight= "1" android:textsize= "14SP" android:gravity= "center_vertical"                    android:paddingleft= "10DP"/> <button android:id= "@+id/follow" Android:layout_width= "Wrap_content" android:layout_height= "Match_parent" android:t                    Extsize= "14SP" android:gravity= "Center_vertical|right" android:paddingright= "10DP" android:paddingleft= "10DP"/> </LinearLayout> </LinearLayout> </c Om.sothree.slidinguppaneL.slidinguppanellayout></relativelayout> 
Workaround 1:

Duplicate ID issues are related to using nested fragments incorrectly.

From: http://developer.android.com/about/versions/android-4.2.html#NestedFragments

Note: You cannot inflate the layout to a fragment when the layout contains <fragment> . Only nested fragments are supported when dynamically added to a fragment.

So you fragment_map.xml cannot include directly com.google.android.gms.maps.MapFragment . Replace it with the FrameLayout ID map_container and try something like this:

public class MyFragmentWithMap extends Fragment {    private MapFragment fragment;    private GoogleMap map;    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment_map, container, false);    }    @Override    public void onActivityCreated(Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        FragmentManager fm = getChildFragmentManager();        fragment = (MapFragment) fm.findFragmentById(R.id.map_container);        if (fragment == null) {            fragment = MapFragment.newInstance();            fm.beginTransaction().replace(R.id.map_container, fragment).commit();        }    }    @Override    public void onResume() {        super.onResume();        setUpMapIfNeeded();    }    // ...}

Please note that use getChildFragmentManager .

Use GoogleMaps crash app when replacing with Fragmentmanager

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.