android之Fragment的bug解決,androidfragment

來源:互聯網
上載者:User

android之Fragment的bug解決,androidfragment

最近做一個功能,當第二次進入一個介面的時候出現一個奇怪的bug,如下面詳細的log資訊:

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)
如上的log資訊可以知道:載入了一個(Duplicate)重複的Fragment(CaptionPropertiesFragment)。問題是代碼應該是沒有問題的!具體看看我系的代碼:

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;    }

從報錯資訊可以知道出錯的代碼:final View rootView = inflater.inflate(R.layout.captioning_preview, container, false);

那麼報錯資訊說是載入了重複的Fragment(CaptionPropertiesFragment)。實際上這個重複的Fragment在這個layout檔案裡面布局的:R.layout.captioning_preview。

很顯然,就是第一次進入這個介面,再退出的時候,這個用過的Fragment(CaptionPropertiesFragment)沒有remove掉!從而出現了兩個問題:

(1)為什麼會出現這個Fragment(CaptionPropertiesFragment)沒有remove,

(2)怎麼解決這個問題!

        在activity中不同的frament之間項目替換的時候,FragmentManager只會remove和add這些frament,然而這些frament裡面自己載入的frament(這裡就是我們的CaptionPropertiesFragment)是沒有被remove. 很顯然這是一個缺陷!因為後一個frament(CaptionPropertiesFragment)很明顯是依賴與他的父frament的,應該同時遞迴的remove.

        那麼如何解決這個問題呢!很顯然就是在不用這個frament(ToggleCaptioningPreferenceFragment)的時候把他裡面載入的frament給remove掉!這個操作在ToggleCaptioningPreferenceFragment的onDestroyView()裡面就可以解決問題了!如下代碼:

@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();}} }}
      這裡要注意f.isDestroyed()這樣來判斷是否這個FragmentManager處於Destroyed,如果不加這個判斷的話,橫豎屏切換時候會出錯!

     注意:對於這個問題,網上有各種無效的解決辦法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.