Android 禁用按钮\u正抛出空指针
我想禁用对话框上的肯定按钮,但无法。这是我的对话:Android 禁用按钮\u正抛出空指针,android,Android,我想禁用对话框上的肯定按钮,但无法。这是我的对话: AlertDialog dialog = new AlertDialog.Builder(this) .setTitle(R.string.delete_db_on_exit) .setCustomTitle(getLayoutInflater().inflate(R.layout.custom_dialog, null)) .setPositiveButton(R.string.confirm_
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle(R.string.delete_db_on_exit)
.setCustomTitle(getLayoutInflater().inflate(R.layout.custom_dialog, null))
.setPositiveButton(R.string.confirm_button_text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
_application.setDeleteDatabaseOnExit();
dialog.cancel();
//Navigate back to main activity
}
})
.create();
dialog.getButton(dialog.BUTTON_POSITIVE).setEnabled(false);
dialog.show();
即使我只是设置了正数按钮,但当我引用它时,我会得到一个空指针异常。这是因为我引用它的地方吗?Android对话框是否有文档中未注明的重大更改
我注意到,我无法做他们在当前工作中注意到的任何事情 我从您的问题中了解到的是::单击对话框中的肯定按钮后,您试图取消对话框
而不是::
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle(R.string.delete_db_on_exit)
.setCustomTitle(getLayoutInflater().inflate(R.layout.custom_dialog, null))
.setPositiveButton(R.string.confirm_button_text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
_application.setDeleteDatabaseOnExit();
dialog.cancel();
//Navigate back to main activity
}
})
.create();
dialog.getButton(dialog.BUTTON_POSITIVE).setEnabled(false);
dialog.show();
试试这个:
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle(R.string.delete_db_on_exit)
.setCustomTitle(getLayoutInflater().inflate(R.layout.custom_dialog, null))
.setPositiveButton(R.string.confirm_button_text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.create();
dialog.getButton(dialog.BUTTON_POSITIVE).setEnabled(false);
dialog.show();
也将代码放在对话框调用之后……
_application.setDeleteDatabaseOnExit();
//Navigate back to main activity
我认为您不能将其放在对话框按钮操作中(如果可能,请使用标志)希望有帮助 我需要在
dialog.show()之后调用getButton(AlertDialog.BUTTON\u正值)
发件人:
致:
嘿,德弗拉斯!我遇到的问题是,dialog.getButton(dialog.BUTTON_POSITIVE).setEnabled(false)代码>行将抛出空指针异常,因为没有按钮\u正值
。
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
dialog.show();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);