![](/img/trans.png)
[英]spring boot app crashing on cloud foundry 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
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first
錯誤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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.