开发者

Problem with AlertDialog Button in android

开发者 https://www.devze.com 2023-03-28 04:12 出处:网络
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 int

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 give开发者_如何学编程s 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();


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();

    } });


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()


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号