Android Fragment bug solution: androidfragment

Source: Internet
Author: User

Android Fragment bug solution: androidfragment

Recently, I made a function. A strange bug occurs when I enter the interface for the second time, as shown in 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: Duplicate id 0x7f0b0034, tag null, or parent id 0xffffffff with another fragment for com.android.settings.accessibility.CaptionPropertiesFragment10-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)
The preceding log information indicates that a Duplicate (Duplicate) Fragment (CaptionPropertiesFragment) is loaded ). The problem is that the Code should be okay! Take a look at the code of my system:

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 savedInstanceState) {        final View rootView = inflater.inflate(R.layout.captioning_preview, container, false);        // We have to do this now because PreferenceFrameLayout looks at it        // only when the view is added.        if (container instanceof PreferenceFrameLayout) {            ((PreferenceFrameLayout.LayoutParams) rootView.getLayoutParams()).removeBorders = true;        }        return rootView;    }

You can see the error code from the error message: final View rootView = inflater. inflate (R. layout. captioning_preview, container, false );

The error message indicates that the repeated Fragment (CaptionPropertiesFragment) is loaded ). In fact, this repeated Fragment is laid out in the layout file: R. layout. captioning_preview.

Obviously, this is the first time you enter this interface and exit, this used Fragment (CaptionPropertiesFragment) was not removed! As a result, two problems occur:

(1) Why does this Fragment (CaptionPropertiesFragment) fail to be removed,

(2) how to solve this problem!

When different frament projects in the activity are replaced, FragmentManager will only remove and add these frament, but the frament loaded by itself in these frament (here is our CaptionPropertiesFragment) is not removed. obviously, this is a defect! Because the next frament (CaptionPropertiesFragment) is obviously dependent on its parent frament and should be recursively removed at the same time.

How can this problem be solved! Obviously, this frament (ToggleCaptioningPreferenceFragment) is removed from it! This operation can solve the problem in onDestroyView () of ToggleCaptioningPreferenceFragment! 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();}} }}
Pay attention to f. isDestroyed () to determine whether the FragmentManager is in Destroyed. If this judgment is not added, an error will occur during screen switching!

Note: There are various ineffective solutions on the Internet for this problem.

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.