繁体   English   中英

如果某事警报对话框上的按钮单击问题android

[英]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.makeText(getApplicationContext(), "Review",
                    Toast.LENGTH_SHORT).show();
                }
              });
              error.show(); 
            }
        }
   }

然后我有一个else语句,如果没有错误会发生什么

我在eclipse的第4行出现错误“构造函数AlertDialog.Builder(new View.OnClickListener(){})未定义”

该行带有: error = new AlertDialog.Builder(this);

当应该传递Context时传递了View.OnClickListener

当您在匿名类中时,不能将this作为Context传递。

正如Scienceprodigy所说,错误是在匿名类中未定义“ this”。 但是您仍然可以进行这项工作。 假设您正在尝试在某些活动中使用类名称MyActivity进行此操作。 只要这样做:

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

由于使用匿名声明,因此您传递的是onClickListener()而不是Context 更改为此:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM