AlertDialog Button does not exist - android

Suddenly (without any changes in this project code) I started to obtained an error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{<package>}: java.lang.IllegalArgumentException: Button does not exist
that error points to a method which wasn't yet called.
private void dialog(String title, String content){
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle(title);
alertDialog.setMessage(content);
alertDialog.setCancelable(true);
alertDialog.setButton(1, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
I tried to copy and use that code in other project - same result, and it was working not long ago (same target API etc.). Any idea what I'm overlooking?

Don't hardcode 1 in setButton(...). Use the constants found in the DialogInterface class to specify which button:
DialogInterface.BUTTON_NEGATIVE
DialogInterface.BUTTON_POSITIVE
DialogInterface.BUTTON_NEUTRAL

Change this line:
alertDialog.setButton(1, "OK", new DialogInterface.OnClickListener() {
To one of these:
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "OK", new DialogInterface.OnClickListener() {
alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
You can look at the Android Documentation on the DialogInterface and on the AlertDialog to look at the setButton methods.
You could also replace BUTTON_POSITIVE, BUTTON_NEGATIVE, and BUTTON_NEUTRAL with their constant values: -1, -2, and -3, respectively.
So for example:
// positive button
alertDialog.setButton(-1, "OK", new DialogInterface.OnClickListener() {

Related

How would I make a basic alert?

I have a if then statement. If the if then statement is true, I want an Android system to fire a basic alert with one button that says ok.
I have done plenty of research, but nothing works.
this is how you create Alert in android
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Write your message here.");
builder.setNeutralButton(
"Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.create().show()

My neutral Ok button is not showing in my alert dialog for Samsung S5 and J700

Trying to build a mobile application and have tested across some devices, but when it comes to Samsung S5 Notebook and J700 mobile phones, both have been failing because they have failed to display the neutral button in the application, please who might have the slightest clue to solving this particular issue. Thanks.
Here is a sample code below;
new AlertDialog.Builder(context)//new Alert Dialog
.setTitle("Delete entry")//set title
.setMessage("Are you sure you want to delete this entry?")
.setNeutralButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})`enter code here`
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
just replace with this -
AlertDialog.Builder builder = new AlertDialog.Builder(context)//new Alert Dialog
.setTitle("Delete entry")//set title
.setMessage("Are you sure you want to delete this entry?")
.setNeutralButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setIcon(android.R.drawable.ic_dialog_alert);
// .show();
AlertDialog d = builder.create();
d.show();
Maybe you should try with .setPositiveButton this should work.
Check if you are using AlertDialog from support library?

What kind of grammar it is in Android?

Dialog dialog = new AlertDialog.Builder(Activity01.this)
.setTitle("Login hint")
.setMessage("Here needs your login!")
.setPositiveButton(...)
.setNeutralButton(...
).create();
What kind of grammar it is? I cannot understand why those dots are one by one? And the create() is for Builder() or for setNeutralButton()?
Thanks!
Builder is a static inner class of AlertDialog. Each call returns this allowing you to chain methods. Finally you call create() to create the actual dialog. This is basic Java and has little to do with Android, besides the fact that Android uses this pattern a lot.
setTitle, setMessage are the methods of the DialogBox.
you can also write
Dialog dialog = new AlertDialog.Builder(Activity01.this);
dialog.setTitle("Login hint")
dialog.setMessage("Here needs your login!")
dialog.setPositiveButton(...)
dialog.setNeutralButton(...)
dialog .create();
if you want more clarification about this you can visit this
.setPositiveButton(...) refers you want to pass text to displayed and write the logic for click events.
Refer here:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();

DialogResult in normal java class and return result

I am working on an android project where I am trying to show a AlertDialog in a separate normal java class and return the result that the user enters. I can display the dialog fine but the problem I am having is it always returns the value before the dialog has had one of the buttons pressed.
Below is the code that calls the function in the standard java class to show the dialog
private void showDiagreeError()
{
Common common = new Common(this);
boolean dialogResult = common.showYesNoDialog();
Toast.makeText(getApplicationContext(), "Result: " + dialogResult, Toast.LENGTH_LONG).show();
}
And below is the code that shows the actual dialogue
public boolean showYesNoDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure you do not want to agree to the terms, if you choose not to, you cannot use Boardies Password Manager")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialogResult = true;
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialogResult = false;
}
});
AlertDialog alert = builder.create();
alert.show();
return dialogResult;
}
dialogResult is a global variable visible throughout the class and being set to false. As soon as the dialog is shown the toast message is shown showing the result is false, but I was expecting the return statement to block until the user has pressed one of the buttons too set the variable to the correct value.
How can I get this to work.
After many hours hunting through the inner depths of google pages, I found this Dialogs / AlertDialogs: How to "block execution" while dialog is up (.NET-style).
It does exactly the job I was after and tested to make sure there are no ANR errors, which there isn't

Problem with my custom dialog which is having dynamic data

I have a usecase like repeatedly calling the same dialog box with different values. I am using the same dialog creating code for that. First time the sent data is populated to dialog box. but next time the dialog box not getting rebuilt with different values when i call the same for next time.
Code is here
dialog.setContentView(R.layout.orderdialog);
dialog.setTitle("Selected Item");
dialog.setCanceledOnTouchOutside(true);
System.out.println(selected); // here i am sending different values eachtime. But not updating in dialog.
TextView selectedItem = (TextView)dialog.findViewById(R.id.itemName);
selectedItem.setText(selected);
You can use the android alert builder to show dynamic data:
new AlertDialog.Builder(this)
.setTitle("your title name")
.setMessage("here you can write your dynamic data as string or integer")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(/* don't remember the signature by heart... */) {
// continue with delete
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(/* don't remember the signature by heart... */) {
// do nothing
}
})
.show();
Instead of calling
showDialog(id);
and creating dialog in oncreatDialog function
create the dialog and show it in you on click function itself:
like this:
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("State");
builder.setItems(m_arrStateNames, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
m_nSelStateIdx = which;
showState();
dialog.dismiss();
}
});
builder.show();
}
});

Categories

Resources