Thursday, August 23, 2012

Using Dialogs and Java1.5 vs Java1.6

Trying to display a MessageBox to the user of my awesome app.
Found a simple example on (thanksgoditexists) Google.
The code looks as follows :


AlertDialog ad = new AlertDialog.Builder(this).create();  
ad.setCancelable(false); 
ad.setMessage("GPS Disabled");  
ad.setButton(DialogInterface.BUTTON_POSITIVE,"OK", new DialogInterface.OnClickListener() {  
@Override  
public void onClick(DialogInterface dialog, int which) {  
               dialog.dismiss();                      
           }  
});  
ad.show();

By compiling this code Eclipse gave me the following error :

method onClick(DialogInterface, int) of type new DialogInterface.OnClickListener(){} must override a superclass method

Here is why :

Eclipse is defaulting to Java 1.5 and you have classes implementing interface methods (which in Java 1.6 can be annotated with @Override, but in Java 1.5 can only be applied to methods overriding a superclass method).
Go to your project/ide preferences and set the java compiler level to 1.6 and also make sure you select JRE 1.6 to execute your program from eclipse.


Happy coding!

No comments:

Post a Comment