关闭对话框后再次打开对话框时,应用程序崩溃并给出 'java.lang.IllegalStateException'

App crashing giving 'java.lang.IllegalStateException' when opening a dialog again after closing it

我有一个对话框,我应该在其中填写 EditText 中的一些详细信息。如果在编辑文本为空时单击肯定按钮,则会显示带有消息的 Snackbar 并关闭对话框。但是,当我再次打开对话框时,应用程序崩溃并给出:java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first 错误。

这是我膨胀视图的方式:

LayoutInflater inflater = this.getLayoutInflater();
addVenueDialog = inflater.inflate(R.layout.add_venue_dialog, null);

这是打开对话框并检查编辑文本是否为空的 java 代码:

case R.id.nav_add_venue:
            if (dialog == null) {

                LayoutInflater inflater = this.getLayoutInflater();
                View addVenueDialog = inflater.inflate(R.layout.add_venue_dialog, null);

                vName = (EditText) addVenueDialog.findViewById(R.id.vName);
                vAddress = (EditText) addVenueDialog.findViewById(R.id.vAddress);

                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Title");
                builder.setView(addVenueDialog);
                builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (isNetworkAvailable()) {
                            if (vName.getText().toString().isEmpty()) {
                                Snackbar snackbar = Snackbar
                                        .make(coordinatorLayout, "V name cannot be empty", Snackbar.LENGTH_SHORT);
                                snackbar.show();
                            } else if (vAddress.getText().toString().isEmpty()) {
                                Snackbar snackbar = Snackbar
                                        .make(coordinatorLayout, "V address cannot be empty", Snackbar.LENGTH_SHORT);
                                snackbar.show();
                            } else {
                                mDatabase.child("vs").child(user.getUid()).child("V name").setValue(vName.getText().toString());
                                mDatabase.child("vs").child(user.getUid()).child("V address").setValue(vAddress.getText().toString());
                            }
                        } else {
                            Snackbar snackbar = Snackbar
                                    .make(coordinatorLayout, "No internet connection", Snackbar.LENGTH_SHORT);
                            snackbar.show();
                        }
                    }
                });
                dialog = builder.create();
            }
            dialog.show();
            break;

我不知道为什么当我关闭对话框后再次打开对话框时应用程序崩溃了。

请告诉我。

一个简单的解决方案是保留 AlertDialog 的全局实例并重新使用它:

//global
private AlertDialog dialog;

现在在开关盒中:

case R.id.nav_add_venue:
    if(dialog == null) {
        LayoutInflater inflater = this.getLayoutInflater(); 
        View addVenueDialog = inflater.inflate(R.layout.add_venue_dialog, null); 

        builder.setView(addVenueDialog); 

        final EditText vName = (EditText) addVenueDialog.findViewById(R.id.vName); 
        final EditText vAddress = (EditText) addVenueDialog.findViewById(R.id.vAddress); 

        // Other code //
        dialog = builder.create();
     }
     dialog.show();
     break;

记得dismiss对话框onDestroy方法来避免内存泄漏:

public void onDestroy() {
   super.onDestroy();
   if(dialog != null) {
     dialog.dismiss();
   }
 }