android之Fragment的bug解决
来源:程序员人生 发布时间:2014-12-24 08:51:33 阅读次数:3605次
最近做1个功能,当第2次进入1个界面的时候出现1个奇怪的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信息可以知道:加载了1个(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。
很明显,就是第1次进入这个界面,再退出的时候,这个用过的Fragment(CaptionPropertiesFragment)没有remove掉!从而出现了两个问题:
(1)为何会出现这个Fragment(CaptionPropertiesFragment)没有remove,
(2)怎样解决这个问题!
在activity中不同的frament之间项目替换的时候,FragmentManager只会remove和add这些frament,但是这些frament里面自己加载的frament(这里就是我们的CaptionPropertiesFragment)是没有被remove. 很明显这是1个缺点!由于后1个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,如果不加这个判断的话,横竖屏切换时候会出错!
注意:对这个问题,网上有各种无效的解决办法。
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠