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



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

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

swipeView.setPageControl(pageControl); 

将滑动视图设置为页面控件的父级。在设置这样的东西之前尝试

以确保
pageControl.getParent();

也许您不应该在活动布局中包含页面控件。取而代之的是这个

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

这样做

pageControl = new PageControl(this);

和比

swipeView.setPageControl(pageControl);

应该工作。

相关内容

  • 没有找到相关文章

最新更新