I would like to prompt the user to give me input in my android application using a dialog. this is what I have found:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText();
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
but this gives me :
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
is there any problem on my code it seems like a null argument is passed on the dialog but I can't find out what is the problem.
When I ran your code in a new project, it worked fine. So probably "this" that you are using
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With