Issue with creating an AlertDialog
I seem to be having an issue creating an AlertDialog. What I am trying to do is create a custom AlertDialog that pops up when a specific RadioButton is clicked. Here is my relevant code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_Minutes = -1;
m_this=this;
String prefName = getString(R.string.prefsfile);
SharedPreferences settings = getSharedPreferences(prefName, 0);
String defaultTextBackMessage = settings.getString("textBackMessage", getString(R.string.defaultTextBackMessage));
EditText txtMessage = (EditText)findViewById(R.id.editText1);
txtMessage.setText(defaultTextBackMessage);
final Button button = (Button)findViewById(R.id.button1);
final RadioButton manualButton = (RadioButton)findViewById(R.id.radio0);
final RadioButton button15 = (RadioButton)findViewById(R.id.radio1);
final RadioButton button30 = (RadioButton)findViewById(R.id.radio2);
final RadioButton c开发者_高级运维ustomButton = (RadioButton)findViewById(R.id.radio3);
manualButton.setChecked(true);
customButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Context c = v.getContext();
LayoutInflater factory = LayoutInflater.from(v.getContext());
final View minuteEntryView = factory.inflate(R.layout.customtime, null);
AlertDialog ad = AlertDialog.Builder(c).create(); // this is the trouble line
}
});
I am getting an error at the AlertDialog ad = AlertDialog.Builder(c).create();
line. The error I am getting is The Method Builder(Context) is undefined for the type AlertDialog
. Clearly, however, the Google API Docs does have a Builder constructor. What am I doing wrong?
should't you say this...
AlertDialog ad = new AlertDialog.Builder(c).create();
you forgot new
keyWord..
As you can clearly see, its saying no Method found, which means you are calling its constructor in usual way but in the way you call a method.
you need to override onCreateDialog(int id) method in your Activity. Inside that method you should create your Dialog object and from your RadioButton's onClick event you should call the dialog by using showDialog(id) method.
See below code:
@Override
protected Dialog onCreateDialog(int id)
{
// TODO Auto-generated method stub
AlertDialog dialog = null;
AlertDialog.Builder builder = null;
builder = new AlertDialog.Builder(this);
switch(id)
{
case USERNAME_PASSWORD_EMPTY:
builder.setMessage("Please Enter Username and Password.");
builder.setCancelable(false);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
//Do what you want to do when user clicks OK button of dialog
}
});
dialog = builder.create();
break;
}
return dialog;
}
You must call this dialog by using showDialog(id) method like below:
showDialog(USERNAME_PASSWORD_EMPTY);
精彩评论