无法使AlertDialog正常工作 [英] Can't get an AlertDialog to work

查看:123
本文介绍了无法使AlertDialog正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个onLongClickListener,它在被调用时会重置一些值. 我想添加一个alertDialog来检查用户是否确实要重置所有值.但是我没有让它工作的喜悦.

I have an onLongClickListener that resets some values when called. I would like to ad an alertDialog to check if the user really does want to reset all the values. However I am having no joy making it work.

重设部分本身可以正常工作,但是如果我尝试添加AlertDialog,则会出现以下错误:

The reset section works fine on it's own but if I try to add the AlertDialog I get the following error:

此行有多个标记 -构造函数AlertDialog.Builder(new View.OnLongClickListener(){})是 不明确的 -断点:SatFinder [line:174]-onLongClick(View)

Multiple markers at this line - The constructor AlertDialog.Builder(new View.OnLongClickListener(){}) is undefined - Line breakpoint:SatFinder [line: 174] - onLongClick(View)

这到底是什么意思,我该如何解决? 非常感谢.

What exactly does this mean and how can I fix it? Many thanks.

下面是代码部分.请注意,该警报在此示例中没有任何用处.克服以上错误后,我将对其进行更改.

Below is the section of code. Please note that the alert does nothing useful in this example. I will change that after I get past the error above.

    resetAll = new OnLongClickListener() {

   @Override
   public boolean onLongClick(View v) {

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

       // set the message to display
       alertbox.setMessage("This is the alertbox!");

       // set a positive/yes button and create a listener
       alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

           // do something when the button is clicked
           public void onClick(DialogInterface arg0, int arg1) {
               Toast.makeText(getApplicationContext(), "'Yes' button clicked", Toast.LENGTH_SHORT).show();
           }
       });

       // set a negative/no button and create a listener
       alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {

           // do something when the button is clicked
           public void onClick(DialogInterface arg0, int arg1) {
               Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show();
           }
       });

       alertbox.show();

    // Resets all values and radio buttons
    pos1_deg.setText("0.0");
    pos2_deg.setText("0.0");
    pos1_az.setText("0.0");
    pos2_az.setText("0.0");
    targetDeg.setText("0.0");
    blurg.setText("----");

    radio1.setChecked(false);
    radio2.setChecked(false);
    radio3.setChecked(false);
    radio1E.setChecked(true);
    radio2E.setChecked(true);
    radio3E.setChecked(true);

    Toast.makeText(getApplicationContext(), 
      "Reset", Toast.LENGTH_LONG).show();

    return true;
   }

};

推荐答案

问题是该行代码:

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

实际上是在实现接口OnLongClickListener的匿名内部类内部. AlertDialog的参数.Builder()构造函数必须是 Context 对象. this作为此处的参数,指的是不扩展Context的匿名内部类对象.我猜您发布的代码片段在 Activity 对象中,在这种情况下,请将行更改为:

is actually inside of anonymous inner class which implements the interface OnLongClickListener. The argument to the AlertDialog.Builder() constructor must be a Context object. this as an argument here, refers to the anonymous inner class object, which does not extend Context. I'm guessing that your posted code fragment is inside an Activity object, in which case, change the line to:

AlertDialog.Builder alertbox = new AlertDialog.Builder(OuterClass.this);

其中 OuterClass 是此方法位于其中的Activity类的名称.这是用于引用定义内部类的对象的语法.

where OuterClass is the name of your Activity class that this method is inside. This is the syntax used to refer to the object that an inner class is defined in.

这篇关于无法使AlertDialog正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆