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

标签 android android-layout android-view android-alertdialog illegalstateexception

我通过设置我的自定义 View 创建了 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);

这是在 onNavigationItemSelected 上使用的 AlertDialog

               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 1 , question 2 , question 3 中回答的大部分解决方案,但不幸的是没有用

最佳答案

从其父 View 中删除 View 并将其重新添加到您的对话框中。在向对话框添加 View 之前添加以下行

删除

if(view.getParent()!=null)
        ((ViewGroup)view.getParent()).removeView(view); 

关于android - IllegalStateException:当我第二次尝试显示 AlertDialog 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55249729/

相关文章:

android - 如何以双分辨率获取屏幕截图?

android - java.lang.IllegalStateExeception : could not find method in activity class 错误

java - Android 中谷歌地图的聚类

android - 双向数据绑定(bind) : View is missing user defined type

android - Recyclerview 设置 clipChildren

android - 如何在 eclipse 中配置 ADT 插件

android - 预加载 .so 库文件的 Android 应用无法升级

java - 为什么一些 Android 代码在以前的 API 级别上工作而不是应该的?

android - 键盘出现时平滑滚动布局?

android - 如果它是在其父级之外绘制的,是否可以检测到 View 点击?