• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps The constructor AlertDialog.Builder is undefined

cr5315

Android Enthusiast
I am trying to make an alert dialog, but after I entered all the code, Eclipse gave me an error on a line that reads
Code:
AlertDialog.Builder builder = [COLOR="Red"]new AlertDialog.Builder(this)[/COLOR];
More specifically new AlertDialog.Builder(this)
The error says, "The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined.
I can't find any solutions on developer.android.com, but I might just be bad at looking. I used the Google example and the same thing happened.
Here is the the rest of the code.
Code:
Button aboutButton = (Button) findViewById(R.id.Button06);
	aboutButton.setOnClickListener(new View.OnClickListener(){
		public void onClick(View v) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("The Crossing Version 0.1")
			       .setCancelable(true)
			       .setNeutralButton("Close", new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			                crossingActivity.this.finish();
			           }
			       
			           
			       });
			AlertDialog alert = builder.create();
		}
 
I think its because the AlertDialog.Builder is inside a inner class(aboutButton.setOnClickListener), try changing to: new AlertDialog.Builder(TheNameOfYourClass.this);
 
Thanks for the reply. I actually figured that out this morning while looking at some dialog box examples.
 
Just a side note, as long as its not within an onClickListener() of any type (ex Long click, etc) this will work just fine, for example, just in a method in the application, calling it with just "this" will work just fine, just so you know!
 
That dialog is in an onClickListener, which was the cause of the problem. Thanks for the tip
 
Button aboutButton = (Button) findViewById(R.id.Button06);
aboutButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
new AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("The Crossing Version 0.1")
.setCancelable(true)
.setNeutralButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
crossingActivity.this.finish();
}


});
AlertDialog alert = builder.create();
}
 
Back
Top Bottom