DropDownMenu icon indicating copy to clipboard operation
DropDownMenu copied to clipboard

动态添加数据刷新控件内容不会再崩溃了 看这。。。

Open ptrtony opened this issue 7 years ago • 6 comments

动态添加数据刷新控件的时候应该对每个容器进行判断,子控件不为空应该先移除子控件再添加子控件,不然就会出现app闪退崩溃的现象,或者会出现数据叠加显示问题

代码附上: public void setDropDownMenu(@NonNull List<String> tabTexts, @NonNull List<View> popupViews, @NonNull View contentView) {

    if(tabTexts.size() != popupViews.size()) {
        throw new IllegalArgumentException("params not match, tabTexts.size() should be equal popupViews.size()");
    } else {
        if (this.tabMenuView.getChildCount()>0)this.tabMenuView.removeAllViews();
        for(int i = 0; i < tabTexts.size(); ++i) {
            this.addTab(tabTexts, i);
        }

        if (this.containerView.getChildAt(0)!=null){
            this.containerView.removeViewAt(0);
        }

        this.containerView.addView(contentView, 0);
        this.maskView = new View(this.getContext());
        this.maskView.setLayoutParams(new android.widget.FrameLayout.LayoutParams(-1, -1));
        this.maskView.setBackgroundColor(this.maskColor);
        this.maskView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               DropDownMenu.this.closeMenu();
            }
        });

        if (this.containerView.getChildAt(1)!=null){
            this.containerView.removeViewAt(1);
        }
        this.containerView.addView(this.maskView, 1);
        this.maskView.setVisibility(8);
        if(this.containerView.getChildAt(2) != null) {
            this.containerView.removeViewAt(2);
        }

        this.popupMenuViews = new FrameLayout(this.getContext());
        this.popupMenuViews.setLayoutParams(new android.widget.FrameLayout.LayoutParams(-1, (int)((float) DisplayUtils.getScreenSize(this.getContext()).y * this.menuHeighPercent)));
        this.popupMenuViews.setVisibility(8);
        this.containerView.addView(this.popupMenuViews, 2);
        if (popupMenuViews.getChildCount()>0)popupMenuViews.removeAllViews();
        for(int i = 0; i < popupViews.size(); ++i) {
            ((View)popupViews.get(i)).setLayoutParams(new android.view.ViewGroup.LayoutParams(-1, -2));
            this.popupMenuViews.addView((View)popupViews.get(i), i);
        }

    }
}

ptrtony avatar Jul 19 '18 09:07 ptrtony

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

wwzww avatar Sep 11 '18 06:09 wwzww

现在我项目中都是用的这段代码 没出现java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 有可能是你的子View没有移除干净导致的 你在检查下

ptrtony avatar Sep 11 '18 06:09 ptrtony

或者调用的时候直接判断containerView有没有子View 如果有调用)containerView.removeAllViews();应该也是可以的

ptrtony avatar Sep 11 '18 06:09 ptrtony

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 你可以看下你add的view是不是同一个引用

androidty avatar Sep 03 '19 10:09 androidty

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

出现同样的问题,加了以上判断也没用。大概是添加的 子view本身存在父容器吧。比如 SmartRefreshLayout,这种问题该怎么解决。

NanaGithub avatar Oct 09 '19 00:10 NanaGithub

https://github.com/dongjunkun/DropDownMenu/issues/1

看到这个,终于理解了,我的布局是在xml中,这样的话就很不方便了,看来得换种方式了。

NanaGithub avatar Oct 09 '19 01:10 NanaGithub