我正在製作具有自定義EditText字段的ALert對話框。如何從AlertDialog中刪除視圖Android
我做了一個View
變量,然後將其與我的自定義EditText
字段關聯。
requestView = inflater.inflate(R.layout.send_request,null);
然後我補充說,以我的AlertDialog
alert.setView(requestView);
此後,我加入的onClick方法要我的按鈕進行警告對話框行動..
chatRequestbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alert.setPositiveButton("Send", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
request = requestMsg.getText().toString();
send();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.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:4417)
at android.view.ViewGroup.addView(ViewGroup.java:4258)
at android.view.ViewGroup.addView(ViewGroup.java:4230)
at android.support.v7.app.AlertController.setupCustomContent(AlertController.java:647)
at android.support.v7.app.AlertController.setupView(AlertController.java:463)
at android.support.v7.app.AlertController.installContent(AlertController.java:226)
at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:257)
at android.app.Dialog.dispatchOnCreate(Dialog.java:395)
at android.app.Dialog.show(Dialog.java:294)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:955)
at com.buckydroid.anonchat.User$3.onClick(User.java:86)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22433)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6126)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
我雖然使視圖爲空,並再次添加視圖,同時單擊按鈕將解決問題。但同樣的問題一次又一次..
你能提供代碼示例你如何創建和顯示AlertDialog? – Grimmy
AlertDialog.Builder alert = new AlertDialog.Builder(this); @Grimmy – Doge
當你顯示對話框時,你是否每次重新創建它? – Grimmy