繁体   English   中英

警报对话框出现问题,因为它无法识别警报对话框的启动

[英]issue with alert dialog box, due to it not recognizing the initiation of the alert box

我正在尝试创建一个对话框,单击该图像时将显示该对话框,但是我的代码似乎存在一些问题。 代码如下所示。 我已经完成了对话框的导入,但是似乎不起作用,因为在启动新对话框时出现错误。

 ImageButton ib = (ImageButton)findViewById(R.id.imageButton1);
    ib.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
             AlertDialog ad = new AlertDialog.Builder(this)
            .setMessage("Blah blah blah.\n Fine pring.\n Do you accept all our terms and conditions?")
            .setIcon(R.drawable.ic_launcher)
            .setTitle("Terms of Service")
            .setNeutralButton("back", this)
            .setCancelable(false)
            .create();

            ad.show();
        }
    });

问题是您在网上this的引用

AlertDialog ad = new AlertDialog.builder(this)

实际上是指不扩展Context的匿名内部类View.OnClickListener 所以,就在您的上方

ib.setOnClickListener(new View.OnClickListener() {

使某事生效

final Context context = this;

然后将aforemention builder调用更改为read

AlertDialog ad = new AlertDialog.builder(context)

ImageButton ib = (ImageButton)findViewById(R.id.imageButton1);
final Context context = this;
ib.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
         AlertDialog ad = new AlertDialog.Builder(context)
        .setMessage("Blah blah blah.\n Fine pring.\n Do you accept all our terms and conditions?")
        .setIcon(R.drawable.ic_launcher)
        .setTitle("Terms of Service")
        .setNeutralButton("back", this)
        .setCancelable(false)
        .create();

        ad.show();
    }
});

而且我(也许是崇高的)假设是,该代码块确实驻留在实际上扩展了Context的类中。

暂无
暂无

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

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