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

时间:2014-04-08 12:22:59

标签: java android

我有一个ViewPager,它是弹出窗口的一部分。当我第一次打开一个弹出窗口时,一切都运行良好,但是当我第二次使用相同的数据打开它时,我有一个IllegalStateException错误。

这是一个堆栈跟踪:

  

04-08 15:17:48.370:E / AndroidRuntime(10929):致命异常:主要   04-08 15:17:48.370:E / AndroidRuntime(10929):   java.lang.IllegalStateException:指定的子节点已经有了   家长。您必须首先在孩子的父母上调用removeView()。 04-08   15:17:48.370:E / AndroidRuntime(10929):at   android.view.ViewGroup.addViewInner(ViewGroup.java:3337)04-08   15:17:48.370:E / AndroidRuntime(10929):at   android.view.ViewGroup.addView(ViewGroup.java:3208)04-08   15:17:48.370:E / AndroidRuntime(10929):at   android.support.v4.view.ViewPager.addView(ViewPager.java:920)04-08   15:17:48.370:E / AndroidRuntime(10929):at   android.view.ViewGroup.addView(ViewGroup.java:3165)04-08   15:17:48.370:E / AndroidRuntime(10929):at   android.view.ViewGroup.addView(ViewGroup.java:3145)04-08   15:17:48.370:E / AndroidRuntime(10929):at   com.componentix.imwizard.SamplePagerAdapter.instantiateItem(SamplePagerAdapter.java:22)   04-08 15:17:48.370:E / AndroidRuntime(10929):at   android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)04-08   15:17:48.370:E / AndroidRuntime(10929):at   android.support.v4.view.ViewPager.populate(ViewPager.java:783)04-08   15:17:48.370:E / AndroidRuntime(10929):at   android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1280)   04-08 15:17:48.370:E / AndroidRuntime(10929):at   android.support.v4.view.ViewPager.computeScroll(ViewPager.java:1176)   04-08 15:17:48.370:E / AndroidRuntime(10929):at   android.view.ViewGroup.drawChild(ViewGroup.java:2729)04-08   15:17:48.370:E / AndroidRuntime(10929):at   android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)04-08   15:17:48.370:E / AndroidRuntime(10929):at   android.view.View.buildDrawingCache(View.java:10693)04-08   15:17:48.370:E / AndroidRuntime(10929):at   android.view.ViewGroup.drawChild(ViewGroup.java:2747)04-08   15:17:48.370:E / AndroidRuntime(10929):at   android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)04-08   15:17:48.370:E / AndroidRuntime(10929):at   android.view.View.draw(View.java:10981)04-08 15:17:48.370:   E / AndroidRuntime(10929):at   android.view.View.getDisplayList(View.java:10417)04-08 15:17:48.370:   E / AndroidRuntime(10929):at   android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2597)   04-08 15:17:48.370:E / AndroidRuntime(10929):at   android.view.View.getDisplayList(View.java:10380)04-08 15:17:48.370:   E / AndroidRuntime(10929):at   android.view.HardwareRenderer $ GlRenderer.draw(HardwareRenderer.java:842)   04-08 15:17:48.370:E / AndroidRuntime(10929):at   android.view.ViewRootImpl.draw(ViewRootImpl.java:1910)04-08   15:17:48.370:E / AndroidRuntime(10929):at   android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1634)   04-08 15:17:48.370:E / AndroidRuntime(10929):at   android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)04-08   15:17:48.370:E / AndroidRuntime(10929):at   android.os.Handler.dispatchMessage(Handler.java:99)04-08   15:17:48.370:E / AndroidRuntime(10929):at   android.os.Looper.loop(Looper.java:137)04-08 15:17:48.370:   E / AndroidRuntime(10929):at   android.app.ActivityThread.main(ActivityThread.java:4430)04-08   15:17:48.370:E / AndroidRuntime(10929):at   java.lang.reflect.Method.invokeNative(Native Method)04-08   15:17:48.370:E / AndroidRuntime(10929):at   java.lang.reflect.Method.invoke(Method.java:511)04-08 15:17:48.370:   E / AndroidRuntime(10929):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:792)   04-08 15:17:48.370:E / AndroidRuntime(10929):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:559)04-08   15:17:48.370:E / AndroidRuntime(10929):at   dalvik.system.NativeStart.main(原生方法)

这是我的代码:

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);
    }

}

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

问题在于你的适配器方法instantiateItem
你打电话给container.addView(v);

但每个View只能有一个父级,
所以它只能通过addView(...)添加到容器一次。

第一次打开弹出窗口时,一切正常,因为v
那个时间没有父母。但是当你第二次打开popupwinow时,
它再次将视图添加到容器中。这可以解决错误。

如果关闭弹出视图或从中删除所有子视图,请尝试销毁视图 container.removeAllViews()

它应该适用。

答案 1 :(得分:2)

问题是第二次在container.addView(v)中添加View v时。

我发现解决方案添加了这个:

        if(v.getParent()!=null)
        ((ViewGroup)v.getParent()).removeView(v);

在container.addView(v)之前