报错java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

public class ViewPagerAdpater extends PagerAdapter {

private List<View> views;


public ViewPagerAdpater(List<View> views) {

this.views = views;

}


/**

* 判断是否显示的是同一张图片

*/

@Override

public boolean isViewFromObject(View arg0, Object arg1) {

// TODO Auto-generated method stub

return arg0 == arg1;

}


/**

* 向viewgroup中添加对象

*/

@Override

public Object instantiateItem(ViewGroup container, int position) {

container.addView(views.get(position));

return views.get(position);

}


/**

* 移除不显示的图片

*/

@Override

public void destroyItem(ViewGroup container, int position, Object object) {

Log.e("destroyitem", position + "");

container.removeView(views.get(position));

}


/**

* 要显示图片的数量

*/

@Override

public int getCount() {

// TODO Auto-generated method stub

return views.size();

}


}

//报的错java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

//下面附上主程序,解释一下就是想做一个引导页,可总是只能显示一张图片,滑动屏幕就报错求大神解答!!

public class MainActivity extends Activity implements OnPageChangeListener,OnClickListener{

private ViewPager vp_views;

private ViewPagerAdpater viewPagerAdpater;

private List<View> views;

private int[] imageresources={R.drawable.photo_4,R.drawable.photo_5,R.drawable.photo_6,R.drawable.photo_7};

private ImageView[] paints;

private LinearLayout ll_paints;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initviewPager();

initpaints();

vp_views.setOnPageChangeListener(this);

}

public  void initpaints() {

ll_paints=(LinearLayout) findViewById(R.id.ll_layout);

paints=new ImageView[ll_paints.getChildCount()];

for (int i = 0; i < ll_paints.getChildCount(); i++) {

paints[i]=((ImageView)ll_paints.getChildAt(i));

paints[i].setTag(i);

paints[i].setImageResource(R.drawable.gc_step_unfocus);

paints[i].setOnClickListener(this);

}

paints[0].setImageResource(R.drawable.gc_step_focused);

}

public void initviewPager() {

vp_views=(ViewPager) findViewById(R.id.vp_views);

views=new ArrayList<View>();

LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

ImageView imageView=new ImageView(this);

for(int i:imageresources){

imageView.setImageResource(i);

Log.e("imageView", i+"");

Log.e("imageView's parent", imageView.getParent()+"");

imageView.setLayoutParams(params);

views.add(imageView);

}

viewPagerAdpater=new ViewPagerAdpater(views);

vp_views.setAdapter(viewPagerAdpater);

}

@Override

public void onPageScrollStateChanged(int arg0) {

// TODO Auto-generated method stub

}

@Override

public void onPageScrolled(int arg0, float arg1, int arg2) {

// TODO Auto-generated method stub

}

@Override

public void onPageSelected(int arg0) {

for (int i = 0; i < ll_paints.getChildCount(); i++) {

paints[i]=((ImageView)ll_paints.getChildAt(i));

paints[i].setTag(i);

paints[i].setImageResource(R.drawable.gc_step_unfocus);

}

paints[arg0].setImageResource(R.drawable.gc_step_focused);

}

@Override

public void onClick(View v) {

int i=(Integer) v.getTag();

vp_views.setCurrentItem(i);

}

}


呵呵哒这都被用
浏览 3637回答 1
1回答

sunyaox

这个异常,是传参数类型不正确发生的,比如你的方法需要传如一个String 类型的参数,但你在调用时却传如的不是String类型的参数。自己检查下吧
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java