问题

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();

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.

Thanks every one!

回答1:

When you override OnCreateView in your RouteSearchFragment class, do you have the

if(view != null) {

return view;

}

code segment?

If so, removing the return statement should solve your problem.

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();

}

}

}

回答2:

Sorry to post to an old question but I was able to fix it using a totally different solution. I was getting this exception but I changed the first line of my onCreatView override from this:

View result = inflater.inflate(R.layout.customer_layout, container);

...to this:

View result = inflater.inflate(R.layout.customer_layout, container, false);

I have no idea why but using the override that accepts the boolean as the third param fixed it. I think it tells the Fragment and/or Activity not to use the "container" as the parent of the newly-created View.

回答3:

I have facing this issue many time.

Please add following code for resolve this issue :

@Override

public void onDestroyView() {

super.onDestroyView();

if (view != null) {

ViewGroup parentViewGroup = (ViewGroup) view.getParent();

if (parentViewGroup != null) {

parentViewGroup.removeAllViews();

}

}

}

Thanks

回答4:

If you have this statement..

View view = inflater.inflate(R.layout.fragment1, container);//may be Incorrect

Then try this.. Add false as third argument.. May be it could help..

View view = inflater.inflate(R.layout.fragment1, container, false);//correct one

回答5:

I had this code in a fragment and it was crashing if I try to come back to this fragment

if (mRootView == null) {

mRootView = inflater.inflate(R.layout.fragment_main, container, false);

}

after gathering the answers on this thread, I realised that mRootView's parent still have mRootView as child. So, this was my fix.

if (mRootView == null) {

mRootView = inflater.inflate(R.layout.fragment_main, container, false);

} else {

((ViewGroup) mRootView.getParent()).removeView(mRootView);

}

hope this helps

回答6:

It also happens when the view returned by onCreateView() isn't the view that was inflated.

Example:

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;

Fix:

return rootView;

Instead of:

return textView; // or whatever you returned

回答7:

I had this problem and couldn't solve it in Java code. The problem was with my xml.

I was trying to add a textView to a container, but had wrapped the textView inside a LinearLayout.

This was the original xml file:

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

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"/>

Now with the LinearLayout removed:

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"/>

This didn't seem like much to me but it did the trick, and I didn't change my Java code at all. It was all in the xml.

回答8:

You are adding a View into a layout, but the View already is in another layout. A View can't be in more than one place.

回答9:

This solution can help :

public class FragmentItem extends Android.Support.V4.App.Fragment

{

View rootView;

TextView textView;

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

if (rootView != null)

{

ViewGroup parent = (ViewGroup)rootView.Parent;

parent.RemoveView(rootView);

} else {

rootView = inflater.Inflate(Resource.Layout.FragmentItem, container, false);

textView = rootView.FindViewById(Resource.Id.textViewDisplay);

}

return rootView;

}

}

回答10:

I solved it by setting attachToRoot of inflater.inflate() to false.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_overview, container, false);

return view;

}

回答11:

in your xml file you should set layout_width and layout_height from wrap_content to match_parent. it's fixed for me.

android:id="@+id/container"

android:layout_width="match_parent"

android:layout_height="match_parent" />

来源:https://stackoverflow.com/questions/10007094/java-lang-illegalstateexception-the-specified-child-already-has-a-parent

java match parent_java.lang.IllegalStateException: The specified child already has a parent相关推荐

  1. 安卓java.lang.IllegalStateException: The specified child already has a parent.解决方案

    在使用ViewPager的时候遇到一个错误java.lang.IllegalStateException: The specified child already has a parent. You ...

  2. 关于viewpager 里嵌套 listview 同时实现翻页功能的“java.lang.IllegalStateException: The specified child...异常处理...

    这几天做项目用到了ViewPager,因为它可以实现左右划动多个页面的效果,然后 再每个页面里使用ListView,运行时总是出现"PagerAdapter java.lang.Illega ...

  3. Android IllegalStateException: The specified child already has a parent问题解决办法

    最近遇到一个很让人头疼的问题,使用viewpager动态添加页面或者删除页面时出现了问题(java.lang.IllegalStateException: The specified child al ...

  4. java.lang.IllegalStateException: Error starting child错误的产生与解决

    经过网友解惑, 一般原因有: 1,jar版本不一致 2,servlet配置重复(xml+注解) 3,servlet配置映射少了'/',(路径错误) 严格意义上我的错误是属于第3种, 错在哪呢? 你们看 ...

  5. IDEA 启动报错:java.lang.IllegalStateException: failed to create a child event loop 问题解决

    遇到的问题 昨天电脑自动更新了,今天死活启动不了项目,看到idea的提示是: idea日志如下 根据提示说 查看idea的启动日志: 2018-11-20 17:35:42,010 [ 46737] ...

  6. Java.lang.IllegalStateException Activity has been destroyed

    12-07 11:13:14.030 13836-13836/com.xxx.xxx.android E/error.out: 检测到严重错误,程序即将退出java.lang.RuntimeExcep ...

  7. 转:java.lang.IllegalStateException异常产生的原因及解决办法

    地址:http://jorton468.blog.163.com/blog/static/72588135201102441617287/ 问题描述: 错误类型大致为以下几种: java.lang.I ...

  8. java.lang.IllegalStateException: Cannot modify managed objects outside of a write transaction. in /U

    错误内容如下 java.lang.IllegalStateException: Cannot modify managed objects outside of a write transaction ...

  9. 批量下载的实现及java.lang.IllegalStateException异常

    在工作流的一张表单里可能会有多个步骤上传附件,在用户的待办中往往会存在多条带有附件的任务,如果一一打开并且点击下载链接下载,不仅费时,而且繁琐,用户体验较差. OA系统采用的是FastDFS做为文件服 ...

最新文章

  1. 多媒体容器格式概述①
  2. conda 修改缓存路径
  3. ajax datatype_JavaScript学习笔记(二十七) ajax及ajax封装
  4. 数据库对象管理 (表)
  5. linux cat cd,linux 文件系统命令 cat cd chmod
  6. 总结构建子类对象时的顺序
  7. Python 分离路径和文件名,分离文件名和后缀
  8. Swift - 计算次方(2的N次方,2的随机次方)
  9. java udp 服务器方_Java的UDP通信的小程序,UDP服务器
  10. java future_Java并发编程之异步Future机制的原理和实现
  11. 夜神模拟器:adb命令
  12. 2019年上半年软件设计师上午真题及答案解析
  13. 有些软件,听着听着就没了...
  14. 【VBA研究】调用API实现汉字简繁互换
  15. 超美二次元响应式引导页源码
  16. 项目敏捷管理模式有哪几种_敏捷团队中有效沟通的5种模式
  17. abortonerror_DCB结构
  18. 6个越南主流社交媒体软件简单介绍
  19. 绵阳南山中学计算机老师邱浩,还原“博士论文走红”的中科院博士:学成还乡衣着朴素...
  20. 物联网网线POE供电主控设计方案

热门文章

  1. 我在Google用AI研究基因,入门从吴恩达的课程开始
  2. 第十五章:使用SpringBoot validator让数据更真实
  3. Android面试,与Service交互方式
  4. 云服务器租用成本节约在哪里?
  5. 栈操作与栈帧 (转)
  6. 64位 linux 中 oracle 11g dbca 报 out of memory 错误
  7. Java对象锁和类锁全面解析(多线程synchronized关键字)
  8. 字符集不同导致的ORA-00972
  9. 听同事讲 Bayesian statistics: Part 1 - Bayesian vs. Frequentist
  10. Pig安装及简单使用(pig0.12.0 hadoop2.2.0)