RunLoop
RunLoop

Reputation: 20376

Issue setting AlertDialog

I am trying to display an AlertDialog, but I get a compile error of (ambiguous) on the following line:

alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", null);

How should I set the button?

Upvotes: 0

Views: 102

Answers (3)

Manolis Proimakis
Manolis Proimakis

Reputation: 1785

Based on the android reference from this link

http://developer.android.com/reference/android/app/AlertDialog.html,

you can see that that there are two methods for setButton with different parameters

1)

public void setButton (int whichButton, CharSequence text, DialogInterface.OnClickListener listener)

and

2)

public void setButton (int whichButton, CharSequence text, Message msg)

so compiler does not know which of these methods you want as you pass a null as the third parameter so it throws an ambiguous compiler error.

Try to pass this as the third parameter if you want it to be null: (DialogInterface.OnClickListener) null

Or you can use the dedicated methods setPositiveButton() and setNegativeButton() of alertDialog.

Upvotes: 0

bofredo
bofredo

Reputation: 2348

 AlertDialog.Builder mAlertDialogBuilder = new AlertDialog.Builder(this.activity);

                mAlertDialogBuilderTablet.setTitle("put your title here")
                        .setMessage("put your question here")
                        .setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which)                          {                                      
                                Log.i(TAG,"clicked YES");
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Log.i(TAG,"clicked NO");
                            }
                        });
                AlertDialog alertDialog = alertDialogBuilderTablet.create();
                alertDialog.show();

Upvotes: 1

Maury
Maury

Reputation: 604

            AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
        dlgAlert.setMessage(getResources().getString(R.string.err_connection));
        dlgAlert.setTitle(getResources().getString(R.string.err_connection_header));
        dlgAlert.setPositiveButton(getResources().getString(R.string.ok),
            new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog, final int which) {
                    finish();
                }
            });
        dlgAlert.setCancelable(true);
        dlgAlert.create().show();

Upvotes: 2

Related Questions