簡體   English   中英

嘗試設置頁面控件時如何調用removeView()?

[英]How to call removeView() when trying to set page control?

我試圖在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()或如何避免此異常?

大概吧

swipeView.setPageControl(pageControl); 

將swipeView設置為pageControl的父級。 嘗試在設置像這樣的東西之前確定

pageControl.getParent();

也許您不應該在活動布局中包含pageControl。 相反,這

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

做這個

pageControl = new PageControl(this);

然后

swipeView.setPageControl(pageControl);

應該管用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM