在onCLickListener()中添加带Ok按钮的消息框

时间:2017-01-14 13:59:50

标签: android messagebox android-applicationinfo

我正在尝试通过关注帖子添加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);

1 个答案:

答案 0 :(得分:2)

你需要使用

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

因为new View.OnClickListener() {是一个匿名类,而this此处指的是anonymous类而不是Activity