开发者

if something Alert Dialog on button click issue android

b.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        if(bob1i + bih1i > 4 || bob2i + bih2i > 4){
            error = new AlertDialog.Builder(this);
            error.setMessage("No more than four bags per team are allowed./n"
                +"Please review your scores.");

            error.setNeutralButton("Ok",
              new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    Toast.makeTex开发者_StackOverflow社区t(getApplicationContext(), "Review",
                    Toast.LENGTH_SHORT).show();
                }
              });
              error.show(); 
            }
        }
   }

i then have an else statement for what happens if there is no error

i get the error at line 4 "The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined" in eclipse


The line with: error = new AlertDialog.Builder(this);

is passing a View.OnClickListener when it should be passing a Context.

You can't pass this as a Context when you're inside an anonymous class.


As Scienceprodigy said, the error is that "this" is undefined within an anonymous class. But you can still make this work. Let's say you're trying to do this in some Activity with the class name MyActivity. Just do this:

error = new AlertDialog.Builder(MyActivity.this);


You're passing an onClickListener() instead of a Context because of the use of anonymous declaration. Change it to this:

b.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        if(bob1i + bih1i > 4 || bob2i + bih2i > 4){
            error = new AlertDialog.Builder(getApplicationContext());

            error.setMessage("No more than four bags per team are allowed./nPlease review your scores.");

            error.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    Toast.makeText(getApplicationContext(), "Review", Toast.LENGTH_SHORT).show();
                }
            });
            error.show();   
        }
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜