无法使AlertDialog工作

Ent*_*024 0 android android-alertdialog

我有一个onLongClickListener,在调用时重置一些值.我想发一个alertDialog来检查用户是否确实要重置所有值.然而,我没有任何快乐使它工作.

重置部分工作正常,但如果我尝试添加AlertDialog,我会收到以下错误:

此行的多个标记 - 构造函数AlertDialog.Builder(new View.OnLongClickListener(){})未定义 - 行断点:SatFinder [行:174] - onLongClick(查看)

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

下面是代码部分.请注意,此示例中的警报没有任何用处.在我超越上述错误后,我会改变它.

    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;
   }

};
Run Code Online (Sandbox Code Playgroud)

rob*_*x44 12

问题是这行代码:

AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
Run Code Online (Sandbox Code Playgroud)

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

AlertDialog.Builder alertbox = new AlertDialog.Builder(OuterClass.this);
Run Code Online (Sandbox Code Playgroud)

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