IllegalStateException:当我第二次尝试显示AlertDialog时

问题描述 投票:0回答:1

我创建了AlertDialog并设置了我的自定义视图,当我第一次点击显示时,一切看起来都不错,但第二次我得到了java.lang.IllegalStateException

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/linearLayout"
    >

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/logo04"
        android:layout_gravity="center"
        />

    <!--<view style="@style/Divider" />-->

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginBottom="15dp"
        android:text="@string/app_name"
        android:textSize="20sp"
        android:textColor="@color/black"
        />


    <!--<view style="@style/Divider" />-->

    <TextView
        android:id="@+id/versionCode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:textColor="@color/black"
        android:textSize="20sp"
        />

    <!--<view style="@style/Divider" />-->

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/contactUs"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:layout_margin="15dp"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/contactEmail"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:layout_margin="15dp"
        android:autoLink="email"
        />

</LinearLayout>

onCreate方法下的膨胀布局

viewGroup = findViewById(R.id.linearLayout);
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.about, viewGroup);

这里是关于使用AlertDialog的onNavigationItemSelected

               case R.id.about:
                if(viewGroup != null){
                    viewGroup.removeAllViews();
                }
                AlertDialog.Builder aboutDialog = new AlertDialog.Builder(this);
                aboutDialog.setCancelable(true)
                        .setPositiveButton("OK", null)
                        .setView(view);

                final AlertDialog alertd = aboutDialog.create();
                        alertd.show();
        }

第二次输出点击about

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:4939)
        at android.view.ViewGroup.addView(ViewGroup.java:4770)
        at android.view.ViewGroup.addView(ViewGroup.java:4742)
        at androidx.appcompat.app.AlertController.setupCustomContent(AlertController.java:657)
        at androidx.appcompat.app.AlertController.setupView(AlertController.java:475)
        at androidx.appcompat.app.AlertController.installContent(AlertController.java:233)
        at androidx.appcompat.app.AlertDialog.onCreate(AlertDialog.java:279)
        at android.app.Dialog.dispatchOnCreate(Dialog.java:403)
        at android.app.Dialog.show(Dialog.java:302)
        at www.pro_cs_is.com.MainActivity.onNavigationItemSelected(MainActivity.java:547)
        at com.google.android.material.navigation.NavigationView$1.onMenuItemSelected(NavigationView.java:174)
        at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840)
        at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991)
        at com.google.android.material.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:368)
        at android.view.View.performClick(View.java:6311)
        at android.view.View$PerformClick.run(View.java:24833)
        at android.os.Handler.handleCallback(Handler.java:794)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:173)
        at android.app.ActivityThread.main(ActivityThread.java:6653)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:821)

我在question 1question 2question 3的回答中尝试了大部分解决方案,但遗憾的是无法正常工作

android android-layout android-view android-alertdialog illegalstateexception
1个回答
0
投票

从其父视图中删除view并将其重新添加到对话框中。在将视图添加到对话框之前添加以下行

去除

if(view.getParent()!=null)
        ((ViewGroup)view.getParent()).removeView(view); 
© www.soinside.com 2019 - 2024. All rights reserved.