【问题标题】:How to call removeView() when trying to set page control?尝试设置页面控制时如何调用 removeView()?
【发布时间】:2014-03-03 07:57:23
【问题描述】:

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

【问题讨论】:

    标签: android swipeview pagecontrol


    【解决方案1】:

    大概

    swipeView.setPageControl(pageControl); 
    

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

    pageControl.getParent();
    

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

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

    这样做

    pageControl = new PageControl(this);
    

    swipeView.setPageControl(pageControl);
    

    应该可以。

    【讨论】:

    • 显示 System.out.println(pageControl.getParent().getClass()) 的输出;和 System.out.println(pageControl.getParent().equals(swipeView));
    • 我已经添加了 xml 并且响应分别给出了类 android.widget.RelativeLayout & false。
    • 你能看看,让我知道可能的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 2019-03-18
    • 2012-07-22
    • 2019-02-04
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    相关资源
    最近更新 更多