2

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

//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() 或如何避免上述代码中出现此异常?

4

1 回答 1

0

可能

swipeView.setPageControl(pageControl); 

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

pageControl.getParent();

也许您不应该在活动布局中包含 pageControl。取而代之的是

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

做这个

pageControl = new PageControl(this);

然后

swipeView.setPageControl(pageControl);

应该管用。

于 2014-03-03T08:06:57.827 回答