Alert dialog with button throws exception

862 Views Asked by At

I'm showing an alert dialog by doing:

    new AlertDialog.Builder(this)
            .setTitle(R.string.label_searching)
            .setMessage(R.string.label_search_noresults)
            .setCancelable(false)
            .setPositiveButton(DialogInterface.BUTTON_POSITIVE, null)
            .create().show();

However, this exception is thrown:

FATAL EXCEPTION: main android.content.res.Resources$NotFoundException: String resource ID #0xffffffff at android.content.res.Resources.getText(Resources.java:242) at android.content.Context.getText(Context.java:282) at android.app.AlertDialog$Builder.setPositiveButton(AlertDialog.java:487)

When I comment out the following line:

.setPositiveButton(DialogInterface.BUTTON_POSITIVE, null)

The dialog is shown, but obviously there's no button displayed. And I need to show a button in the dialog!!

What am I doing wrong?

5

There are 5 best solutions below

0
On BEST ANSWER

Use it accordingly

  AlertDialog.Builder alertbox = new AlertDialog.Builder(YourActivity.this);

               alertbox.setTitle("Do you want To exit ?");
               alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface arg0, int arg1) { 
                      // finish used for destroyed activity
                       exit();
                   }
               });

               alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface arg0, int arg1) {
                           // Nothing will be happened when clicked on no button 
                           // of Dialog     
                 }
               });

               alertbox.show();
0
On

I think there is problem with DialogInterface.BUTTON_POSITIVE you can found solution at here

0
On

DialogInterface.BUTTON_POSITIVE is a constant, if you check DialogInterface class:

int BUTTON_POSITIVE = -1;

setPositiveButton method received either a textId or a charsequence for its arguement, which is the text that will appear on the positive button. Android cannot find the associated string for the -1 id defined by DialogInterface.

I suggest you define your positive button text on a xml file like you did with your labels for title and message and use it on the first argument.

0
On
private void createAlertDialog() {

     AlertDialog.Builder alrtDialog = new AlertDialog.Builder(
                this);
        alrtDialog.setMessage("your message").setCancelable(false);
        alrtDialog.setPositiveButton("ButtonName",
                new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Do somthing
            }
        });

        alrtDialog.setNeutralButton(R.string.cancel,
                new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                //Do somthing
            }
        });
        alrtDialog.create();
        alrtDialog.show();
    }
0
On

You need to get String id something like this.

new AlertDialog.Builder(this)
    .setTitle(getResources().getString(R.string.label_searching))
    .setMessage(getResources().getString(R.string.label_search_noresults))
    .setCancelable(false)
    .setPositiveButton(DialogInterface.BUTTON_POSITIVE, null)
    .create().show();