Reputation: 7921
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;
}
};
Upvotes: 0
Views: 5619
Reputation: 283
AlertDialog.Builder alertbox = new AlertDialog.Builder(v.getContext());
Upvotes: 0
Reputation: 9314
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.
Upvotes: 11