android - AlertDialog can't show custom layout -
i use following code snippet show custom dialog:
btncancel.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // 1. instantiate alertdialog.builder constructor alertdialog.builder builder = new alertdialog.builder(context); // 2. chain various setter methods set 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 click button, exception:
android.util.androidruntimeexception: requestfeature() must called before adding content @ com.android.internal.policy.impl.phonewindow.requestfeature(phonewindow.java:229) @ com.android.internal.app.alertcontroller.installcontent(alertcontroller.java:234) @ android.app.alertdialog.oncreate(alertdialog.java:337) @ android.app.dialog.dispatchoncreate(dialog.java:355) @ android.app.dialog.show(dialog.java:260) @ com.example.myapp.simpleactivity$2.onclick(simpleactivity.java:108) @ android.view.view.performclick(view.java:4207) @ android.view.view$performclick.run(view.java:17372) @ android.os.handler.handlecallback(handler.java:725) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:5041) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) @ dalvik.system.nativestart.main(native method)
what kind of request need?
you need call setview
instead of setcontentview
:
dialog.setview(r.layout.question);
and set view before creating 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();
Comments
Post a Comment