ViewPager的IllegalStateException运行异常处理

2 篇文章 0 订阅
1 篇文章 0 订阅

开发场景:1.在activity执行oncreate()生命周期时,初始化viewpager控件、viewpager的适配器adapter,adapter的填充数组list;

                    2. 当用户刷新数据的时候,先清空listview,并将新数据填充到list中,调用adapter的notifyDataSetChanged()方法,此时产生运行错误。


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


代码如下:

MainActivity.java

package com.example.androidtest;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;


public class MainActivity extends Activity {
	private ViewPager vp ;
	private VPAdapter vpAdapter;
	private List<View> views;

	@Override  
	public void onCreate(Bundle savedInstanceState) {  
		super.onCreate(savedInstanceState);  
		setContentView(R.layout.activity_main);  
		vp = (ViewPager)findViewById(R.id.vp);
		views = new ArrayList<View>();
		vpAdapter = new VPAdapter(views);
		vp.setAdapter(vpAdapter);
	}

	public void doClick(View v){
		switch (v.getId()) {
		case R.id.add:
			views.clear();
			for (int i = 0; i < 3; i++) {
				ImageView iv = (ImageView)((View)LayoutInflater.from(this).inflate(R.layout.imageview, null)).findViewById(R.id.iv);
				iv.setImageResource(R.drawable.ic_launcher);
				views.add(iv);
			}
			vpAdapter.notifyDataSetChanged();
			break;
		case R.id.del:
			break;
		}
	}
}  

package com.example.androidtest;

import java.util.ArrayList;
import java.util.List;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

public class VPAdapter extends PagerAdapter {
	private List<View> viewList;

	public VPAdapter(List<View> viewList){
		if(viewList!=null){
			this.viewList = viewList;
		}else{
			this.viewList = new ArrayList<View>();
		}
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return viewList.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return arg0==arg1;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView(viewList.get(position));
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position){
		//这里的判断是关键代码,首先我们要判断这个view的父容器是否为null;
		//如果为null,证明是第一次加入,直接添加即可,
		//如果不为null。此时需要先remove掉父容器,再将这个view添加到container中,否则就会产生IllegalStateException异常
		if(viewList.get(position).getParent()==null){
			container.addView(viewList.get(position), 0);
		}else{
			((ViewGroup)viewList.get(position).getParent()).removeView(viewList.get(position));
			container.addView(viewList.get(position));
		}
		return viewList.get(position);
	}
	
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值