我创建了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();
}
第二次输出时,单击大约
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的答案中尝试了大多数解决方案,但很遗憾,此方法不起作用
答案 0 :(得分:0)
从其父视图中删除view
并将其重新添加到对话框中。在将视图添加到对话框之前,添加以下行
要删除
if(view.getParent()!=null)
((ViewGroup)view.getParent()).removeView(view);