Reputation: 8384
I use the following code snippet to show a custom dialog:
btnCancel.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(context);
// 2. Chain together various setter methods to set the dialog
// characteristics
builder.setTitle("Question");
AlertDialog dialog= builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
dialog.dismiss();
CallMethod();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
}).create();
dialog.setContentView(R.layout.question);
dialog.show();
}
});
When I click the button, I get this exception:
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:229)
at com.android.internal.app.AlertController.installContent(AlertController.java:234)
at android.app.AlertDialog.onCreate(AlertDialog.java:337)
at android.app.Dialog.dispatchOnCreate(Dialog.java:355)
at android.app.Dialog.show(Dialog.java:260)
at com.example.MyApp.SimpleActivity$2.onClick(SimpleActivity.java:108)
at android.view.View.performClick(View.java:4207)
at android.view.View$PerformClick.run(View.java:17372)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
What kind of request does it need?
Upvotes: 3
Views: 1023
Reputation: 8384
The code snippet is good, the error is this: the field context was set to another Activity. I changed it to the following and it works now:
AlertDialog.Builder builder = new AlertDialog.Builder(SimpleActivity.this);
Your hints are appreciated.
Upvotes: 0
Reputation: 9996
You need to call setView
instead of setContentView
:
dialog.setView(R.layout.question);
And set view before creating the dialog:
dialog.setView(R.layout.question).create();
[Edit]
AlertDialog dialog= builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
dialog.dismiss();
CallMethod();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
}).setView(R.layout.question).create();
Upvotes: 3
Reputation: 406
It is probably the best to write your own dialog class:
private class CustomDialogClass extends Dialog implements View.OnClickListener {
public CustomDialogClass(Activity a, String picturename) {
super(a);
this.c = a;
this.picturename = picturename;
...
set in the oncreate your layout:
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialoglayout);
and open it like this:
CustomDialogClass cdd = new CustomDialogClass();
cdd.getWindow());
cdd.show();
Upvotes: 0
Reputation: 607
You need to set the custom view before creating the dialog.
Try to use
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setTitle("Question")
.setView()
.setPositiveButton()
.setNegativeButton()
.create().show();
}
});
Upvotes: 1
Reputation: 522
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
You MUST place requestFeature before you set the content view. Example:
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
}
NOT:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
Upvotes: 0