在 onCLickListener() 中添加带有确定按钮的消息框

Adding a message box with Ok button inside an onCLickListener()

我正在尝试通过遵循 post 添加 Ok button 并尝试了该代码,但不知何故它向我显示错误

Builder (android.content.Context) in builder cannot be applied to (anonymous android.view.View.onClickListener)

这是我的代码

submit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            if (Arrays.asList(input).contains("")){
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setMessage("You still have unanswered questions. Please go back");
                alert.setTitle("Notice");
                alert.setPositiveButton("OK",
                        new DialogInterface.OnClickListener(){
                            public void onClick(DialogInterface dialog, int which){

                            }
                        });
            }else {
                Intent in = new Intent(getApplicationContext(),gcfResult.class);
                startActivity(in);


            }
        }
    });

错误出现在 this 行下方的关键字

AlertDialog.Builder alert = new AlertDialog.Builder(this);

你需要使用

 AlertDialog.Builder alert = new AlertDialog.Builder(YourActivityname.this);

因为 new View.OnClickListener() { 是匿名的 class 而这里的 this 指向 anonymous class 而不是你的 Activity