Can't get an AlertDialog to work

我是研究僧i 提交于 2019-12-25 08:36:39

问题


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.

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

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

};

回答1:


The problem is that this line of code:

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

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

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.




回答2:


AlertDialog.Builder alertbox = new AlertDialog.Builder(v.getContext());


来源:https://stackoverflow.com/questions/4148094/cant-get-an-alertdialog-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!