I have some code to create a basic dialog box with several options – if i select a certain option, i get a message saying this has been clicked – i know very basic stuff.
This is my code:
The public method – public void onClick(DialogInterface dialog, int which, boolean isChecked) returns an error stating that it must override a superclass method.
Can anyone tell me what this superclass method is? I can’t seem to find it anywhere.
Thanks in advance.
Chilun
package net.learn2develop.Dialog2;
import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
CharSequence[] items = { "Google", "Apple", "Microsoft" };
boolean[] itemsChecked = new boolean [items.length];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btn_dialog);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(0);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("This is a dialog with some simple text...")
.setPositiveButton("OK", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "OK clicked!", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_SHORT).show();
}
})
.setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() {
@Override
**public void onClick(DialogInterface dialog, int which, boolean isChecked)** {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), items[which] + (isChecked ? " checked!" : " unchecked!"), Toast.LENGTH_SHORT).show();
}
}
)
.create();
}
return null;
}
}
The code looks OK. Check your project properties and see if your project compliance is 1.6. Right click your project ->
Properties-> Under theJava Compiler->Compiler compliance should be set to 1.6. Java 1.5 doesn’t allow the@Overrideannotation use there.