在Android中使用ViewPager时出现java.lang.IllegalStateException异常

3
我有一个ViewPager,它是弹出窗口的一部分。当我第一次打开弹出窗口时,一切正常,但当我使用相同数据第二次打开它时,就会出现IllegalStateException错误。
以下是堆栈跟踪:
04-08 15:17:48.370: E/AndroidRuntime(10929): 致命异常:主线程错误,导致应用程序无法正常运行。错误类型为java.lang.IllegalStateException,提示子元素已经有了其它父级元素,请先从该子元素的当前父级元素中移除该子元素。相关代码如下:android.view.ViewGroup.addViewInner(ViewGroup.java:3337); android.view.ViewGroup.addView(ViewGroup.java:3208); android.support.v4.view.ViewPager.addView(ViewPager.java:920); android.view.ViewGroup.addView(ViewGroup.java:3165); android.view.ViewGroup.addView(ViewGroup.java:3145); com.componentix.imwizard.SamplePagerAdapter.instantiateItem(SamplePagerAdapter.java:22)。这个错误还导致了ViewPager无法正常完成滚动和绘制工作,具体表现为应用程序卡顿或崩溃。

以下是我的代码:

public void showPopupWindow(Drawable picture)
{
    int imageResource = R.drawable.background;
    Drawable image = getResources().getDrawable(imageResource);
    popupLayout = new LinearLayout(context);
    popupLayout.setOrientation(LinearLayout.VERTICAL);
    popupLayout.setBackgroundResource(R.drawable.background);
    LayoutInflater ltInflater = getLayoutInflater();
    RelativeLayout rl = (RelativeLayout)ltInflater.inflate(R.layout.viewpager_layout, null);
    PagerContainer pagerContainer = (PagerContainer)(rl.getChildAt(0));
    CirclePageIndicator circlePageIndicator = (CirclePageIndicator)(rl.getChildAt(1));
    Button button = (Button)(rl.getChildAt(2));

    int maxWidth = 1;
    int maxHeight = 1;
    for (Drawable drawable: bigImages) {
        Bitmap bmp = drawableToBitmap(drawable);
        maxWidth = Math.max(maxWidth, bmp.getWidth());
        maxHeight = Math.max(maxHeight, bmp.getHeight());
    }

    ViewPager viewPager = pagerContainer.getViewPager();
    //viewPager.setBackgroundDrawable(image);

    ArrayList<ImageView> tmpImageViewsWithScreenshots = (ArrayList<ImageView>) imageViewsWithScreenshots.clone();
    SamplePagerAdapter pagerAdapter = new SamplePagerAdapter(tmpImageViewsWithScreenshots);
    viewPager.setAdapter(pagerAdapter);
    viewPager.setCurrentItem(imageIndex);
    circlePageIndicator.setViewPager(viewPager);
    circlePageIndicator.setCurrentItem(imageIndex);

    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int childCount = checkboxLayout.getChildCount();
            for (int i = 0; i < childCount; ++i) {
                RelativeLayout currentLayout = (RelativeLayout) checkboxLayout
                        .getChildAt(i);
                int innerChildCount = currentLayout
                        .getChildCount();
                for (int j = 0; j < innerChildCount; ++j) {
                    View view = currentLayout.getChildAt(j);
                    view.setEnabled(true);
                }
            }
            imageViewLayout.setEnabled(true);
            lockableScrollView.setmScrollable(true);
            back.setEnabled(true);
            next.setEnabled(true);
            popupWindow.dismiss();
        }
    });
    popupLayout.addView(rl);
    LinearLayout.LayoutParams nlp= new LinearLayout.LayoutParams(450, 600);
    rl.setLayoutParams(nlp);
    popupWindow = new PopupWindow(popupLayout,
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    int childCount = checkboxLayout.getChildCount();
    for (int i = 0; i < childCount; ++i) {
        RelativeLayout currentLayout = (RelativeLayout) checkboxLayout
                .getChildAt(i);
        int innerChildCount = currentLayout.getChildCount();
        for (int j = 0; j < innerChildCount; ++j) {
            View view = currentLayout.getChildAt(j);
            view.setEnabled(false);
        }
    }
    next.setEnabled(false);
    back.setEnabled(false);
    imageViewLayout.setEnabled(false);
    lockableScrollView.setmScrollable(false);
    popupWindow
            .showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
    }

这是我适配器的代码:

public class SamplePagerAdapter extends PagerAdapter{

    List<ImageView> pages = null;

    public SamplePagerAdapter(List<ImageView> pages){
        this.pages = pages;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position){
        View v = pages.get(position);
        //((ViewPager) collection).addView(v, 0);
        container.addView(v);
        return v;
    }

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

    @Override
    public int getCount(){
        return pages.size();
    }

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

}

任何想法?

在你的SamplePagerAdapter中尝试这种方式:@Override public boolean isViewFromObject(View view, Object object) {return view == ((View) object);} - M D
@SimplePlan 还是不行。 - Mykhailo Granik
2个回答

3
问题在于你的适配器方法instantiateItem中调用了container.addView(v);,但是每个View只能有一个父级,因此只能通过addView(...)将其添加到容器中一次。
第一次打开弹出窗口时,一切都正常,因为此时v没有父级。但是当您第二次打开弹出窗口时,它会再次将视图添加到容器中。这就创建了错误。
如果关闭弹出窗口,则尝试销毁视图,或使用container.removeAllViews()从中删除所有子视图,然后应该可以解决问题。

是的,我也这么认为,但我该如何解决这个问题呢? - Mykhailo Granik
是的,谢谢,你的解决方案有效。 ViewGroup parent = (ViewGroup) v.getParent(); if (parent != null) { parent.removeAllViews(); } - Mykhailo Granik

2
问题在于第二次调用 container.addView(v) 时添加了 View v。
我已经找到解决方案,只需添加以下代码:
        if(v.getParent()!=null)
        ((ViewGroup)v.getParent()).removeView(v);

在 container.addView(v) 之前


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接