Reputation: 2179
I have set up an AlertDialog
like this:
AlertDialog.Builder noteAlert = new AlertDialog.Builder(ClassName.this);
noteAlert.setTitle("Title");
noteAlert.setMessage("Message");
noteAlert.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// some code
}
});
noteAlert.setNeutralButton("Positive", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// some code
}
});
noteAlert.setNegativeButton("Positive", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// some code
}
});
AlertDialog alertDialog = noteAlert.create();
Button deleteButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
if (someCondition != 1)
// code runs till here
deleteButton.setEnabled(false); // code works on deleting this line
noteAlert.show();
When I run the above code, it works till the if
statement. Then the app crashes (I am assuming getButton()
throws an NPE). I have seen many other answers on SO which give the same code as solution to disabling a button.
And when I comment out the setEnabled()
line, the app works fine (only the button is not disable). So basically I am trying to disable this NegativeButton and it is not working. Can you guys suggest some solution?
LogCat:
07-13 08:01:14.378: D/ViewRootImpl(19779): ViewRoot TouchDown(Absolute) DOWN (380 , 691)
07-13 08:01:14.495: E/dialog(19779): AlertDiablog begins
07-13 08:01:14.495: E/hasnote(19779): 0
07-13 08:01:14.511: E/hasnote(19779): 0
07-13 08:01:14.511: D/AndroidRuntime(19779): Shutting down VM
07-13 08:01:14.511: W/dalvikvm(19779): threadid=1: thread exiting with uncaught exception (group=0x40e392a0)
07-13 08:01:14.519: E/AndroidRuntime(19779): FATAL EXCEPTION: main
07-13 08:01:14.519: E/AndroidRuntime(19779): java.lang.NullPointerException
07-13 08:01:14.519: E/AndroidRuntime(19779): at com.example.sherlockcaldroid2.TestSubjectCalendar$1$2.onClick(TestSubjectCalendar.java:250)
07-13 08:01:14.519: E/AndroidRuntime(19779): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:1 66)
07-13 08:01:14.519: E/AndroidRuntime(19779): at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 08:01:14.519: E/AndroidRuntime(19779): at android.os.Looper.loop(Looper.java:137)
07-13 08:01:14.519: E/AndroidRuntime(19779): at android.app.ActivityThread.main(ActivityThread.java:4849)
07-13 08:01:14.519: E/AndroidRuntime(19779): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 08:01:14.519: E/AndroidRuntime(19779): at java.lang.reflect.Method.invoke(Method.java:511)
07-13 08:01:14.519: E/AndroidRuntime(19779): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
07-13 08:01:14.519: E/AndroidRuntime(19779): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
07-13 08:01:14.519: E/AndroidRuntime(19779): at dalvik.system.NativeStart.main(Native Method)
07-13 08:01:34.089: I/Process(19779): Sending signal. PID: 19779 SIG: 9
Upvotes: 1
Views: 4708
Reputation: 1236
Access the button after dialog.show
. Else it will be null
.
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setTitle("Title");
builder.setMessage("message");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Do Something
}
});
builder.setNegativeButton(context.getResources().getString(R.string.no_text), (dialog, which) -> {
dialog.cancel();
listener.onNoButtonClick();
});
AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(false);
Upvotes: 0
Reputation: 382
This is because the button does not exist at the time of the call deleteButton.setEnabled(false)
. You should move the call noteAlert.show()
before deleteButton.setEnabled(false)
.
Upvotes: 1
Reputation: 6683
Try this solution:
AlertDialog.Builder noteAlert = new AlertDialog.Builder(ClassName.this);
noteAlert.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// some code
}
});
noteAlert.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
if(**some condition**)
{
Button button = builder.getButton(AlertDialog.BUTTON_POSITIVE);
if (button != null) {
button.setEnabled(false);
}
}
}
});
Upvotes: 0
Reputation: 2440
I am pretty sure the view is not inflated until later in the life-cycle (i.e. on calling show()).
I took a quick look at the docs and couldn't find a build() or inflate() method but I would expect that the easiest way is just to move the noteAlert.show() before the button manipulation logic
EDIT: Did you try changing:
noteAlert.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// some code
}
});
to
noteAlert.setButton(DialogInterface.BUTTON_POSITIVE, "Positive", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// some code
}
});
Upvotes: 1