Recently do a function, when the second time into an interface, a strange bug, such as the following detailed log information:
10-01 13:36:23.549:e/androidruntime (14188): Process:com.android.settings, pid:1418810-01 13:36:23.549:e/ Androidruntime (14188): android.view.InflateException:Binary XML file line #43: Error inflating class fragment10-01 13:36 : 23.549:e/androidruntime (14188): at Android.view.LayoutInflater.createViewFromTag (layoutinflater.java:713) 10-01 13:36:23.549:e/androidruntime (14188): at Android.view.LayoutInflater.rInflate (layoutinflater.java:755) 10-01 13:36:23.549:e/androidruntime (14188): at Android.view.LayoutInflater.inflate (layoutinflater.java:492) 10-01 13:36:23.549:e/androidruntime (14188): at Android.view.LayoutInflater.inflate (layoutinflater.java:397) 10-01 13:36:23.549:e/androidruntime (14188): at Com.android.settings.accessibility.ToggleCaptioningPreferenceFragment.onCreateView ( togglecaptioningpreferencefragment.java:69) 10-01 13:36:23.549:e/androidruntime (14188): at Android.app.Fragment.performCreateView (fragment.java:1700) 10-01 13:36:23.549:e/androidruntime (14188): at Android.app. Fragmentmanagerimpl.movetostate (fragmentmanager.java:890) 10-01 13:36:23.549:e/androidruntime (14188): at Android.app.FragmentManagerImpl.moveToState (fragmentmanager.java:1062) 10-01 13:36:23.549:e/androidruntime (14188 ): At Android.app.BackStackRecord.run (backstackrecord.java:698) 10-01 13:36:23.549:e/androidruntime (14188): at Android.app.FragmentManagerImpl.execPendingActions (fragmentmanager.java:1447) 10-01 13:36:23.549:e/ Androidruntime (14188): at Android.app.fragmentmanagerimpl$1.run (fragmentmanager.java:443) 10-01 13:36:23.549:e/ Androidruntime (14188): at Android.os.Handler.handleCallback (handler.java:808) 10-01 13:36:23.549:e/androidruntime ( 14188): At Android.os.Handler.dispatchMessage (handler.java:103) 10-01 13:36:23.549:e/androidruntime (14188): at Android.os.Looper.loop (looper.java:193) 10-01 13:36:23.549:e/androidruntime (14188): at Android.app.ActivityThread.main (activitythread.java:5333) 10-01 13:36:23.549:e/androidruntime (14188): at Java.lang.reflect.Method.invokeNative (Native Method) 10-01 13:36:23.549:e/androidruntime (14188): at Java.lang.reflect.Method.invoke (method.java:515) 10-01 13:36:23.549:e/androidruntime (14188): at Com.android.internal.os.zygoteinit$methodandargscaller.run ( zygoteinit.java:824) 10-01 13:36:23.549:e/androidruntime (14188): at Com.android.internal.os.ZygoteInit.main ( zygoteinit.java:640) 10-01 13:36:23.549:e/androidruntime (14188): at Dalvik.system.NativeStart.main (Native Method) 10-01 13:36:23.549:e/androidruntime (14188): caused by:java.lang.IllegalArgumentException:Binary XML file line #43: Dup Licate ID 0x7f0b0034, tag null, or parent ID 0xFFFFFFFF with another fragment for Com.android.settings.accessibility.Capti onpropertiesfragment10-01 13:36:23.549:e/androidruntime (14188): at Android.app.Activity.onCreateView ( activity.java:4912) 10-01 13:36:23.549:e/androidruntime (14188): at Android.view.LayoutInflater.createViewFromTag ( layoutinflater.java:689)
As the log information above can be known: Loaded A (Duplicate) duplicate fragment (captionpropertiesfragment). The problem is that the code should be no problem! Take a look at the code of my department:
public class Togglecaptioningpreferencefragment extends Fragment {private static final float default_font_size = 48f; Private Captionpropertiesfragment mpropsfragment; Private Subtitleview Mpreviewtext; Private Captioningmanager Mcaptioningmanager; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Mcaptioningmanager = (Captioningmanager) getactivity (). Getsystemservice (Context.captioning_service); } @Override Public View oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstances Tate) {final View Rootview = inflater.inflate (R.layout.captioning_preview, container, false); We have the because preferenceframelayout looks at it//if the view is added. if (container instanceof preferenceframelayout) {(preferenceframelayout.layoutparams) Rootview.getlayoutparam S ()). Removeborders = true; } return rootview; }
From the error message you can know the code of the error: final View Rootview = inflater.inflate (R.layout.captioning_preview, container, false);
Then the error message is that the duplicate fragment (captionpropertiesfragment) is loaded. In fact, this repeating fragment is laid out in this layout file: R.layout.captioning_preview.
Obviously, this is the first time to enter this interface, and then exit, this used fragment (captionpropertiesfragment) did not remove! There are two problems:
(1) Why does this fragment (captionpropertiesfragment) not remove,
(2) How to solve this problem!
When a project is replaced between different frament in the activity, Fragmentmanager will only remove and add these frament, However these frament inside themselves loaded frament (here is our captionpropertiesfragment) is not removed. Obviously this is a flaw! Since the latter frament (captionpropertiesfragment) is obviously dependent on the frament with his father, the remove should be recursive at the same time.
So how to solve this problem! It is clear that the frament loaded inside him is removed when you don't need this frament (togglecaptioningpreferencefragment)! This operation can solve the problem in Togglecaptioningpreferencefragment's Ondestroyview ()! The following code:
@Overridepublic void Ondestroyview () {Super.ondestroyview (); if (mpropsfragment! = null) {Fragmentmanager F = Getfragmentmanager (); if (f! = null &&!f.isdestroyed ()) {final Fragmenttransaction ft = f.begintransaction (); if ( FT! = null) {Ft.remove (mpropsfragment). commit ();}} }}
Here to pay attention to f.isdestroyed () so to determine whether this fragmentmanager is in destroyed, if not add this judgment, the screen will be the same as the switch error!
Note: For this issue, there are various invalid solutions on the Internet.
Bug fixes for Android fragment