java.lang.IllegalStateException: 指定的孩子已经有一个父母 [英] java.lang.IllegalStateException: The specified child already has a parent
问题描述
我正在使用片段,当我第一次实例化片段时.但是第二次我得到了这个例外.我找不到出错的那一行?
I am using fragments, when I instantiate a fragment the first time it it. but the second time I got this exception. I couldn't find the line where I got the error?
04-04 08:51:54.320: E/AndroidRuntime(29713): FATAL EXCEPTION: main
04-04 08:51:54.320: E/AndroidRuntime(29713): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
04-04 08:51:54.320: E/AndroidRuntime(29713): at android.view.ViewGroup.addViewInner(ViewGroup.java:3013)
04-04 08:51:54.320: E/AndroidRuntime(29713): at android.view.ViewGroup.addView(ViewGroup.java:2902)
04-04 08:51:54.320: E/AndroidRuntime(29713): at android.view.ViewGroup.addView(ViewGroup.java:2859)
04-04 08:51:54.320: E/AndroidRuntime(29713): at android.view.ViewGroup.addView(ViewGroup.java:2839)
04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.NoSaveStateFrameLayout.wrap(Unknown Source)
04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.BackStackRecord.run(Unknown Source)
04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.FragmentManagerImpl.execPendingActions(Unknown Source)
04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.FragmentManagerImpl$1.run(Unknown Source)
04-04 08:51:54.320: E/AndroidRuntime(29713): at android.os.Handler.handleCallback(Handler.java:587)
04-04 08:51:54.320: E/AndroidRuntime(29713): at android.os.Handler.dispatchMessage(Handler.java:92)
04-04 08:51:54.320: E/AndroidRuntime(29713): at android.os.Looper.loop(Looper.java:132)
04-04 08:51:54.320: E/AndroidRuntime(29713): at android.app.ActivityThread.main(ActivityThread.java:4126)
04-04 08:51:54.320: E/AndroidRuntime(29713): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 08:51:54.320: E/AndroidRuntime(29713): at java.lang.reflect.Method.invoke(Method.java:491)
04-04 08:51:54.320: E/AndroidRuntime(29713): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
04-04 08:51:54.320: E/AndroidRuntime(29713): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
04-04 08:51:54.320: E/AndroidRuntime(29713): at dalvik.system.NativeStart.main(Native Method)
这是我单击列表片段的元素时执行的操作.
Here are what i do when i click on an element of my list fragment.
// If we are not currently showing a fragment for the new
// position, we need to create and install a new one.
RouteSearchFragment df = RouteSearchFragment.newInstance(index);
// Execute a transaction, replacing any existing fragment
// with this one inside the frame.
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.details_full, df);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
第一次就ok了,我点list中的element2,也ok;但是当我返回 element1 时,我发现了这个错误.
The first time it is Ok, I click element2 from list, it's also ok; but when I return to element1 I got this bug.
谢谢大家!
推荐答案
当你在你的 RouteSearchFragment
类中覆盖 OnCreateView
时,你有
When you override OnCreateView
in your RouteSearchFragment
class, do you have the
if(view != null) {
return view;
}
代码段?
如果是这样,删除 return 语句应该可以解决您的问题.
If so, removing the return statement should solve your problem.
如果您不想重新生成视图数据,您可以保留代码并返回视图,并且 onDestroyView() 方法可以像这样从其父视图中删除此视图:
You can keep the code and return the view if you don't want to regenerate view data, and onDestroyView() method you remove this view from its parent like so:
@Override
public void onDestroyView() {
super.onDestroyView();
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeAllViews();
}
}
}
这篇关于java.lang.IllegalStateException: 指定的孩子已经有一个父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!