android - support.v7.app.AlertDialog throws NullPointerException on dismiss -
i updated appcompat
newly released revision 22.1.0
, changed alertdialog
support.v7.app.alertdialog
. on lollipop device, throws following exception on dismissdialog()
.
java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.class java.lang.object.getclass()' on null object reference @ android.support.v7.internal.app.windowdecoractionbar.getdecortoolbar(windowdecoractionbar.java:248) @ android.support.v7.internal.app.windowdecoractionbar.init(windowdecoractionbar.java:201) @ android.support.v7.internal.app.windowdecoractionbar.<init>(windowdecoractionbar.java:184) @ android.support.v7.app.appcompatdeleg ateimplv7.cre atesupportactionbar(appcompatdelegateimplv7.java:176) @ android.support.v7.app.appcompatdelegateimplbase.getsupportactionbar(appcompatdelegateimplbase.java:85) @ android.support.v7.app.appcompatdelegateimplv7.onstop(appcompatdeleg ateimplv7.java:221) @ android.support.v7.app.appcompatdialog.onstop(appcompatdialog.java:108) @ android.app.dialog.dismissdialog(dialog.java:438) @ android.app.dialog.dismiss(dialog.java:414) @ android.support.v7.app.alertcontroller$buttonhandler.handlemessage(alertcontroller.java:157) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:145) @ android.app.activitythread.main(activitythread.java:5834) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1388) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1183)
how fix it?
(lower-version devices seems work well. happens in lollipop)
+ don't call dismiss()
explicitly in code. dialog throws exception when dismissed back-button or positive/negative button.
++ here code uses v7.app.alertdialog. thank you.
import android.app.dialog; import android.content.dialoginterface; import android.content.dialoginterface.onclicklistener; import android.os.bundle; import android.support.v4.app.dialogfragment; import android.support.v7.app.alertdialog; import android.text.textutils; public class simpleyesnofragment extends dialogfragment { public interface onconfirmlistner { public void onconfirm(); } public static simpleyesnofragment newinstance(string title, string message) { simpleyesnofragment fragment = new simpleyesnofragment(); bundle args = new bundle(); args.putstring("title", title); args.putstring("message", message); fragment.setarguments(args); return fragment; } private onconfirmlistner mlistener; public void setonconfirmlistener(onconfirmlistner l) { mlistener = l; } @override public dialog oncreatedialog(bundle savedinstancestate) { alertdialog.builder b= new alertdialog.builder(getactivity()) .setpositivebutton(r.string.yes, new onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { if(mlistener != null) { mlistener.onconfirm(); } } }) .setnegativebutton(r.string.no, new onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { } }); bundle args = getarguments(); string title = args.getstring("title",null); if(!textutils.isempty(title)) { b.settitle(title); } string message = args.getstring("message",null); if(!textutils.isempty(message)) { b.setmessage(message); } return b.create(); } }
by chance i've noticed project has values-v21/themes.xml
, applies android:theme.material.light.dialog.alert
support.v7.app.alertdialog
. caused weird bug.
using proper appcompat theme, such theme.appcompat.light.dialog.alert
, support.v7.app.alertdialog
solved problem. helping me out.