hay*_*ssi 94 android android-layout android-fragments
我正在使用片段,当我第一次实例化片段时.但第二次我得到了这个例外.我找不到出错的地方?
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)
Run Code Online (Sandbox Code Playgroud)
这是我点击列表片段的元素时所做的事情.
// 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();
Run Code Online (Sandbox Code Playgroud)
第一次它是好的,我从列表中单击element2,它也没关系; 但是当我回到element1时,我遇到了这个bug.
感谢大家!
Pat*_*ick 368
很抱歉发布一个旧问题,但我能够使用完全不同的解决方案来修复它.我得到了这个异常,但我改变了我的onCreatView覆盖的第一行:
View result = inflater.inflate(R.layout.customer_layout, container);
Run Code Online (Sandbox Code Playgroud)
......对此:
View result = inflater.inflate(R.layout.customer_layout, container, false);
Run Code Online (Sandbox Code Playgroud)
我不知道为什么但是使用接受布尔值的覆盖作为第三个参数修复它.我认为它告诉Fragment和/或Activity不要使用"容器"作为新创建的View的父级.HTH!
Har*_*dik 45
我多次面对这个问题.请添加以下代码以解决此问题:
@Override
public void onDestroyView() {
super.onDestroyView();
if (view != null) {
ViewGroup parentViewGroup = (ViewGroup) view.getParent();
if (parentViewGroup != null) {
parentViewGroup.removeAllViews();
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
Dha*_*y M 34
如果你有这个说法..
View view = inflater.inflate(R.layout.fragment1, container);//may be Incorrect
Run Code Online (Sandbox Code Playgroud)
然后尝试这个..添加false作为第三个参数..可能它可以帮助..
View view = inflater.inflate(R.layout.fragment1, container, false);//correct one
Run Code Online (Sandbox Code Playgroud)
Med*_*edo 33
当你OnCreateView在RouteSearchFragment班级中覆盖时,你有吗?
if(view != null) {
return view;
}
Run Code Online (Sandbox Code Playgroud)
代码段?
如果是这样,删除return语句应该可以解决您的问题.
如果您不想重新生成视图数据,可以保留代码并返回视图,而如果您从父节点中删除此视图,则可以使用onDestroyView()方法,如下所示:
@Override
public void onDestroyView() {
super.onDestroyView();
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeAllViews();
}
}
}
Run Code Online (Sandbox Code Playgroud)
use*_*525 21
我在片段中有这个代码,如果我试图回到这个片段,它就会崩溃
if (mRootView == null) {
mRootView = inflater.inflate(R.layout.fragment_main, container, false);
}
Run Code Online (Sandbox Code Playgroud)
在这个帖子上收集答案之后,我意识到mRootView的父级仍然将mRootView作为子级.所以,这是我的修复.
if (mRootView == null) {
mRootView = inflater.inflate(R.layout.fragment_main, container, false);
} else {
((ViewGroup) mRootView.getParent()).removeView(mRootView);
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助
Mat*_*res 16
当onCreateView()返回的视图不是被夸大的视图时,也会发生这种情况.
例:
View rootView = inflater.inflate(R.layout.my_fragment, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.text_view);
textView.setText("Some text.");
return textView;
Run Code Online (Sandbox Code Playgroud)
固定:
return rootView;
Run Code Online (Sandbox Code Playgroud)
代替:
return textView; // or whatever you returned
Run Code Online (Sandbox Code Playgroud)
小智 5
我有这个问题,无法用Java代码解决它.问题出在我的xml上.
我试图将textView添加到容器中,但已将textView包装在LinearLayout中.
这是原始的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
</LinearLayout>Run Code Online (Sandbox Code Playgroud)
现在删除了LinearLayout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>Run Code Online (Sandbox Code Playgroud)
这对我来说似乎并不多,但它完成了诀窍,我根本没有改变我的Java代码.它全部在xml中.
| 归档时间: |
|
| 查看次数: |
101685 次 |
| 最近记录: |