首页 > 代码库 > android之Fragment的bug解决
android之Fragment的bug解决
最近做一个功能,当第二次进入一个界面的时候出现一个奇怪的bug,如下面详细的log信息:
10-01 13:36:23.549: E/AndroidRuntime(14188): Process: com.android.settings, PID: 14188 10-01 13:36:23.549: E/AndroidRuntime(14188): android.view.InflateException: Binary XML file line #43: Error inflating class fragment 10-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.CaptionPropertiesFragment 10-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()里面就可以解决问题了!如下代码:
@Override public 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,如果不加这个判断的话,横竖屏切换时候会出错!
注意:对于这个问题,网上有各种无效的解决办法。
android之Fragment的bug解决