首先在孩子的父母上查看ViewPager removeView()

时间:2013-05-02 02:39:42

标签: android

我目前正在测试Android中的viewpager。创建了一个自定义适配器,但是当我尝试滑动viewpager时出现此错误。任何人都可以帮我这个吗?

public class ViewPagerAdapter extends PagerAdapter {
    Context context;
    int[] flag;
    LayoutInflater inflater;

    public ViewPagerAdapter(Context context, int[] flag) {
        this.context = context;
        this.flag = flag;
    }

    @Override
    public int getCount() {
        return flag.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imgflag;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.viewpager_item,  container, false);
        //ImageView imageView = new ImageView(context);
        //imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        //imageView.findViewById(R.id.flag);
        imgflag = (ImageView) itemView.findViewById(R.id.flag);
        imgflag.setImageResource(flag[position]);
        ((ViewPager) container).addView(imgflag);

        return imgflag;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((ImageView) object);

    }

LOGCAT:

05-02 10:35:26.926: E/AndroidRuntime(4939): FATAL EXCEPTION: main
05-02 10:35:26.926: E/AndroidRuntime(4939): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

编辑推荐理查德:

public class ViewPagerAdapter extends PagerAdapter {
Context context;
int[] flag;
LayoutInflater inflater;
ImageView imgflag;
View itemView;

public ViewPagerAdapter(Context context, int[] flag) {
    this.context = context;
    this.flag = flag;
}

@Override
public int getCount() {
    return flag.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((ImageView) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {


    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    itemView = inflater.inflate(R.layout.viewpager_item,  container, false);
    //ImageView imageView = new ImageView(context);
    //imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    //imageView.findViewById(R.id.flag);
    imgflag = (ImageView) itemView.findViewById(R.id.flag);
    imgflag.setImageResource(flag[position]);

    ((ViewPager) container).addView(itemView);

    //return imgflag;
    return itemView;
}


@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    //((ViewPager) container).removeView((ImageView) object);
    ((ViewPager) container).removeView(itemView);
    //((ViewGroup)imgflag.getParent()).removeView(imgflag);

}

}

LOGCAT:

05-02 10:55:42.786: E/AndroidRuntime(7763): FATAL EXCEPTION: main
05-02 10:55:42.786: E/AndroidRuntime(7763): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.ImageView

1 个答案:

答案 0 :(得分:2)

这是因为你要返回imgFlag而不是itemView。您还需要将itemView添加到寻呼机,而不是添加imgFlag。

inflater将imgFlag的父级设置为itemView,然后尝试将imgFlag添加到viewpager,而不先将其从itemView中删除。

编辑:这很可能是因为:

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((ImageView) object);
}

公共类ViewPagerAdapter扩展了PagerAdapter {     上下文背景;     int []标志;     LayoutInflater inflater;

public ViewPagerAdapter(Context context, int[] flag) {
    this.context = context;
    this.flag = flag;
}

@Override
public int getCount() {
    return flag.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((RelativeLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    ImageView imgflag;

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater.inflate(R.layout.viewpager_item,  container, false);
    //ImageView imageView = new ImageView(context);
    //imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    //imageView.findViewById(R.id.flag);
    imgflag = (ImageView) itemView.findViewById(R.id.flag);
    imgflag.setImageResource(flag[position]);
    ((ViewPager) container).addView(imgflag);

    return imgflag;
}


@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((RelativeLayout) object);

}