alertDialog.getButton() method gives null pointer exception android
Take a look here for the answer : http://code.google.com/p/android/issues/detail?id=6360
As it says in comment #4 you must call show()
on your dialog before you can access the buttons, they are not available beforehand. For an automatic solution on how to modify the buttons as soon as they are ready see Mickeys answer
Android dialog.getButton(AlertDialog.BUTTON_POSITIVE) Null Pointer
The Views
in a Dialog
aren't created until the Dialog
is actually shown; that is, until you call dialog.show()
. The create()
method name is a little misleading. If you try to get a reference to the Positive button before then, it will return null
. Move the Button positiveButton...
code block to after your call to dialog.show()
.
AlertDialog getButton() method returns null
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
}
});
dialog.getButton() is not reachable in Android Kotlin
You should add setTextColor
after show()
;
val alertDialog = AlertDialog.Builder(this).create()
alertDialog.setTitle(getString((R.string.app_name)))
alertDialog.setMessage(getString(R.string.app_name))
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "RESUME"
) { dialog, which -> dialog.dismiss() }
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "ABORT"
) { dialog, which -> dialog.dismiss() }
alertDialog.show()
alertDialog.setCancelable(false)
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(this,R.color.colorPrimary))
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(this,R.color.colorAccent))
can't get a pointer on my button in a dialogfragment
The positive Button
won't actually be created until the Dialog
is shown. Since you're in a DialogFragment
, you're not handling the show()
call on the Dialog
directly. You could set an OnShowListener
on the AlertDialog
, but it's probably simpler to just get the Button
in the DialogFragment
's onStart()
method.
@Override
public void onStart() {
super.onStart();
positiveButton = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
...
}
Related Topics
Using the Limit Statement in a SQLite Query
How to Send Data Message Using Firebase Console
Classloader Referenced Unknown Path: /Data/App/
View the Task's Activity Stack
Android Opengl Texture Compression
How to Show a Marker in Maps Launched by Geo Uri Intent
How to Clear Cookies and Cache of Webview on Android When Not in Webview
Android: How Get the Status-Code of an Httpclient Request
How to Unregister Broadcastreceiver
How to Open My Existing Eclipse Projects in Android Studio
Apache Http Connection with Android 6.0 (Marshmallow)
Android Mediarecorder - "Start Failed: -19"
Using Build Types in Gradle to Run Same App That Uses Contentprovider on One Device