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

时间:2016-09-16 18:47:58

标签: java android layout-inflater illegalstateexception android-inflate

我有一个对话框,我应该在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;

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

请告诉我。

1 个答案:

答案 0 :(得分:1)

一个简单的解决方案是保留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();
   }
 }