Sean
Sean

Reputation: 1123

Problem with AlertDialog Button in android

I have an Alert Dialog that is supposed to set a boolean to true. For setPositiveButton I have the Dialog onclick interface as null. When I add an onClickListener to the setNegativeButtons onclick interface it gives me a compile error saying: The method setNegativeButton(int, DialogInterface.OnClickListener) in the type AlertDialog.Builder is not applicable for the arguments (String, new View.OnClickListener(){})

Here is my code, why am I getting a compile error and how do I fix this? Thanks

new AlertDialog.Builder(ImTracking.this)
    .setMessage(
            Html.fromHtml(getText("http://www.cellphonesolutions.net/im-following-"
                    + getResources().getString(
                            R.string.Country))))
    .setPositiveButton("OK", null)
    // The red squigly is under the .setNegativeButton
    .setNegativeButton("Don't Remind", new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            SharedPreferences prefs= getSharedPreferences("Settings",0);
            SharedPreferences.Editor editor=prefs.edit();
            editor.putBoolean("ImTrackingDontRemind",true);
            editor.commit();
        }
    }).show();

Upvotes: 0

Views: 6992

Answers (3)

Patrick
Patrick

Reputation: 14278

In addition to providing the answer, perhaps it would also be prudent to provide an explanation of why that's the answer. Plus, Sean's question was,

...why am I getting a compile error and how do I fix this?

emphasis mine. While the accepted answer answers the latter question, it does not attempt to answer the former question.

Sean, the onClickListner anonymous inner class you're creating is actually a member function of View since you provided no class name. Your compile error stems from the fact that AlertDialog extends the Dialog class, not the View class and thus has an onClickListner member function with a different function signature:

public abstract void onClick (DialogInterface dialog, int which)

than that of View's onClickListner member function:

public abstract void onClick (View v).

Upvotes: 2

Lalit Poptani
Lalit Poptani

Reputation: 67286

Here is your solution, you did a silly mistake there buddy.

It should not be

.setNegativeButton("Don't Remind", new OnClickListener() 

It should be

.setNegativeButton("Don't Remind", new DialogInterface.OnClickListener()

Upvotes: 5

Manish Burman
Manish Burman

Reputation: 3079

So this is was it should be

  alertDialog.setNegativeButton("Don't Remind", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {

                        // TODO Auto-generated method stub

                        SharedPreferences prefs= getSharedPreferences("Settings",0);
                        SharedPreferences.Editor editor=prefs.edit();
                        editor.putBoolean("ImTrackingDontRemind",true);
                        editor.commit();

    } });

Upvotes: 7

Related Questions