当前位置: 代码迷 >> 综合 >> java.lang.IllegalStateException: The specified child already has a parent. You must call removeView(
  详细解决方案

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView(

热度:104   发布时间:2023-10-19 07:22:27.0
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

上面是报错原因

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.at android.view.ViewGroup.addViewInner(ViewGroup.java:4976)at android.view.ViewGroup.addView(ViewGroup.java:4807)at fr.castorflex.android.verticalviewpager.VerticalViewPager.addView(VerticalViewPager.java:1202)at android.view.ViewGroup.addView(ViewGroup.java:4747)at android.view.ViewGroup.addView(ViewGroup.java:4720)at cn.sobolong.human85.adapter.ViewPagerAdapter.instantiateItem(ViewPagerAdapter.kt:36)at fr.castorflex.android.verticalviewpager.VerticalViewPager.addNewItem(VerticalViewPager.java:729)at fr.castorflex.android.verticalviewpager.VerticalViewPager.populate(VerticalViewPager.java:879)at fr.castorflex.android.verticalviewpager.VerticalViewPager.setCurrentItemInternal(VerticalViewPager.java:447)at fr.castorflex.android.verticalviewpager.VerticalViewPager.setCurrentItemInternal(VerticalViewPager.java:406)at fr.castorflex.android.verticalviewpager.VerticalViewPager.setCurrentItem(VerticalViewPager.java:398)at cn.sobolong.human85.fragments.HomePageFragment$getNews$1$onSuccess$1$2$run$1.run(HomePageFragment.kt:224)at android.os.Handler.handleCallback(Handler.java:808)at android.os.Handler.dispatchMessage(Handler.java:101)at android.os.Looper.loop(Looper.java:166)at android.app.ActivityThread.main(ActivityThread.java:7425)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

这里报错是用的viewpager在适配器里面报的

 fun instantiateItem(container: ViewGroup, position: Int): Any {container?.addView(datas!![position])return datas!![position]}

在addview的时候这句报错,提示是需要removeview()父类  意思是在添加一个子view的时候,然而子view已经有自己的parent,addview的时候又添加了别的parent,要先移除后在添加。具体的列子和详解请看关于异常“The specified child already has a parent. You must call removeView"的解决(举例说明,附源码)这个讲的比较详细

解决方法:

一、解决方法之前附上viewpager的适配器

import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.view.View
import android.view.ViewGroupclass ViewPagerAdapter(private var datas: List<View>?) : PagerAdapter() {fun setDatas(datas: List<View>) {this.datas = datas}override/*** 获得页面的???数*/fun getCount(): Int {return datas!!.size}override/*** 获得相应位置上的view* container  view的容器,其实就是viewpager自身* position 	相应的位??*/fun instantiateItem(container: ViewGroup, position: Int): Any {container?.addView(datas!![position])return datas!![position]}override/*** 判断 view和object的对应关??*/fun isViewFromObject(view: View, `object`: Any): Boolean {return view === `object`}override/*** ??毁对应位置上的object*/fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {if (container is ViewPager) {val viewPager = container as ViewPagervar view: View? = `object` as ViewviewPager.removeView(view)view = null}}override fun getItemPosition(`object`: Any?): Int {return PagerAdapter.POSITION_NONE}
}

这种方法是在

instantiateItem方法里面直接removeall所有的viewgrop
if (v.getParent() != null) {((ViewGroup) v.getParent()).removeView(v);
}
container.addView(v);

这个方法可行,,虽然解决了,,但是有个问题,,我要实现的滚动播放图片,这样相当于删除了我的所有图片,导致最后就只添加了一张图片,有很多图片不显示问题,,所以有问题。

二,这个方法我试了可行

还是在instantiateItem方法的里面修改


1
down vote
@Override
public Object instantiateItem(View arg0, int arg1) {Log.d("instantiateItem", ""+arg0+" "+arg1);try { if(mListViews.get(arg1).getParent()==null)((ViewPager) arg0).addView(mListViews.get(arg1), 0);  else{// I am new to android, it is strange that the view to be added is already bound to a parent// Through trials and error I solve this problem with the following codes// Add that the element of mlistviews is listview in pagerview;((ViewGroup)mListViews.get(arg1).getParent()).removeView(mListViews.get(arg1));((ViewPager) arg0).addView(mListViews.get(arg1), 0); }} catch (Exception e) {   Log.d("parent=", ""+mListViews.get(arg1).getParent()); e.printStackTrace();  }  return mListViews.get(arg1);
}

我试了可行,还没有遇到什么问题。如果还不行就看看这篇文章;

链接地址

 

  相关解决方案