【问题标题】:Adding a message box with Ok button inside an onCLickListener()在 onCLickListener() 中添加带有 Ok 按钮的消息框
【发布时间】:2017-05-29 18:44:58
【问题描述】:

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

【问题讨论】:

  • 这对 View.onClickListener 的引用您可能在活动中使用此代码假设 MyActivity 您应该像这样创建构建器“new AlertDialog.Builder(MyActivity.this)”

标签: android messagebox android-applicationinfo


【解决方案1】:

你需要使用

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

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

【讨论】:

  • @desperateStudent 我很高兴能帮上忙,编码愉快
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-27
  • 2022-10-14
相关资源
最近更新 更多