簡體   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