如何在尝试设置页面控件时调用removeView()?

时间:2014-03-03 07:57:23

标签: android swipeview pagecontrol

我正在尝试在swipeview中设置一些图像,并在该视图中添加一个pagecontrol,如下所示:

//In onCreate
swipeView = (SwipeView) findViewById(R.id.swipe_view);
            swipeView.setOnClickListener(this);
            pageControl = (PageControl) findViewById(R.id.page_control); 
            pageControl.setEnabled(true);
  setPagination();

private void setPagination() {
            for (int i = 0; i < lnoitem.lnoImageList.size(); i++) 
            {
                FrameLayout frame = new FrameLayout(MyAffairDetails.this);
                View child = MyAffairDetails.this.getLayoutInflater().inflate(R.layout.swipe_view_affair, null);
                frame.addView(child);

                final ImageView imgThumb      = (ImageView) child.findViewById(R.id.imgThumb);
                final WebView webViewThumb    = (WebView) child.findViewById(R.id.WebView);

                webViewThumb.getSettings().setLoadWithOverviewMode(true);
                webViewThumb.getSettings().setUseWideViewPort(false);
                webViewThumb.getSettings().setBuiltInZoomControls(false);
                webViewThumb.setVerticalScrollBarEnabled(false);
                webViewThumb.setScrollbarFadingEnabled(false);
                webViewThumb.setScrollContainer(false); 
                webViewThumb.setFocusable(false);
                webViewThumb.setClickable(false);
                webViewThumb.setBackgroundColor(Color.parseColor("#00000000"));

                String path = "";
                String name = lnoitem.lnoImageList.get(i).getBigImage();
                String html = "<body style='margin:0;padding:0;' ><img src='"+name+"' width='100%' height='100%' /></body>";
                webViewThumb.loadDataWithBaseURL(path, html, "text/html", "utf-8", "");

                webViewThumb.setWebViewClient(new WebViewClient() 
                {

                   @Override
                   public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
                       return true;
                   }

                   public void onPageStarted(WebView view, String url) { }

                   @Override
                   public void onPageFinished(WebView view, String url) {
                       imgThumb.setVisibility(View.GONE);
                       webViewThumb.setVisibility(View.VISIBLE);
                   }                  
                });

               swipeView.addView(frame);        

            }
            swipeView.setPageControl(pageControl);
        }
}

这很好但是当我添加一个新图像并再次调用该方法时,我在 swipeView.setPageControl(pageControl)中得到一个例外;

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
at android.view.ViewGroup.addView(ViewGroup.java:3210)
at android.view.ViewGroup.addView(ViewGroup.java:3155)
at android.view.ViewGroup.addView(ViewGroup.java:3131)

的xml:

<RelativeLayout
                    android:id="@+id/xLinea"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="15dp"
                    android:layout_marginLeft="1dp"

                    android:layout_marginRight="1dp"
                    android:gravity="center_horizontal" >

                 <uk.co.jasonfry.android.tools.ui.SwipeView
                        android:id="@+id/swipe_view"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_centerInParent="true"
                        android:layout_marginTop="10dp"
                        android:gravity="center|center_horizontal"
                        android:orientation="horizontal" >
                    </uk.co.jasonfry.android.tools.ui.SwipeView> 

                    <uk.co.jasonfry.android.tools.ui.PageControl
                        android:id="@+id/page_control"
                        android:layout_width="150dp"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/swipe_view"
                     android:gravity="center|center_horizontal"
                     android:layout_centerHorizontal="true"
                        android:layout_marginTop="10dp"
                     android:layout_centerInParent="true"
                        android:orientation="horizontal" >
                    </uk.co.jasonfry.android.tools.ui.PageControl> 
                </RelativeLayout>

任何人都可以告诉我如何调用removeview()或如何避免在上面给出的代码中出现此异常?

1 个答案:

答案 0 :(得分:0)

可行

swipeView.setPageControl(pageControl); 

将swipeView设置为pageControl的父级。 在设置这样的东西之前尝试确保

pageControl.getParent();

也许你不应该在活动布局中包含pageControl。 而不是这个

pageControl = (PageControl) findViewById(R.id.page_control);

这样做

pageControl = new PageControl(this);

而不是

swipeView.setPageControl(pageControl);

应该有用。