android - You need to use a Theme.AppCompat theme (or descendant) with this activity. Change to Theme.AppCompat causes other error -
i use appcompat v22.1.0 in app , use toolbar. fine when use theme.appcompat.light.noactionbar
. when start implement alertdialog
, produce error this:
java.lang.illegalstateexception: need use theme.appcompat theme (or descendant) activity. @ android.support.v7.app.appcompatdelegateimplbase.oncreate(appcompatdelegateimplbase.java:113) @ android.support.v7.app.appcompatdelegateimplv7.oncreate(appcompatdelegateimplv7.java:146) @ android.support.v7.app.appcompatdialog.<init>(appcompatdialog.java:47) @ android.support.v7.app.alertdialog.<init>(alertdialog.java:92) @ android.support.v7.app.alertdialog$builder.create(alertdialog.java:882) @ com.ramabmtr.map.findingmoo.mainactivity.onoptionsitemselected(mainactivity.java:216) @ android.app.activity.onmenuitemselected(activity.java:2572) @ android.support.v4.app.fragmentactivity.onmenuitemselected(fragmentactivity.java:353) @ android.support.v7.app.appcompatactivity.onmenuitemselected(appcompatactivity.java:144) @ android.support.v7.internal.view.windowcallbackwrapper.onmenuitemselected(windowcallbackwrapper.java:99) @ android.support.v7.internal.view.windowcallbackwrapper.onmenuitemselected(windowcallbackwrapper.java:99) @ android.support.v7.internal.app.toolbaractionbar$2.onmenuitemclick(toolbaractionbar.java:74) @ android.support.v7.widget.toolbar$1.onmenuitemclick(toolbar.java:164) @ android.support.v7.widget.actionmenuview$menubuildercallback.onmenuitemselected(actionmenuview.java:740) @ android.support.v7.internal.view.menu.menubuilder.dispatchmenuitemselected(menubuilder.java:802) @ android.support.v7.internal.view.menu.menuitemimpl.invoke(menuitemimpl.java:153) @ android.support.v7.internal.view.menu.menubuilder.performitemaction(menubuilder.java:949) @ android.support.v7.internal.view.menu.menubuilder.performitemaction(menubuilder.java:939) @ android.support.v7.widget.actionmenuview.invokeitem(actionmenuview.java:598) @ android.support.v7.internal.view.menu.actionmenuitemview.onclick(actionmenuitemview.java:139) @ android.view.view.performclick(view.java:4084) @ android.view.view$performclick.run(view.java:16989) @ android.os.handler.handlecallback(handler.java:615) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:4812) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:792) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:559) @ dalvik.system.nativestart.main(native method)
based on error, change theme theme.appcompat
, put this:
<item name="windowactionbar">false</item> <item name="windownotitle">true</item>
to theme. produce same error.
style.xml (old)
<resources> <style name="apptheme" parent="theme.appcompat.light.noactionbar"> <item name="colorprimary">@color/colorprimary</item> <item name="colorprimarydark">@color/colorprimarydark</item> </style> </resources>
style.xml (new)
<resources> <style name="apptheme" parent="theme.appcompat"> <item name="windowactionbar">false</item> <item name="windownotitle">true</item> <item name="colorprimary">@color/colorprimary</item> <item name="colorprimarydark">@color/colorprimarydark</item> </style> </resources>
anyone knows how fix it??
mainactivity.java
package com.ramabmtr.map.findingmoo; import android.content.dialoginterface; import android.graphics.typeface; import android.os.bundle; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbardrawertoggle; import android.support.v7.app.alertdialog; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.recyclerview; import android.support.v7.widget.toolbar; import android.view.view; import android.widget.textview; import android.widget.toast; public class mainactivity extends appcompatactivity { private toolbar toolbar; private textview toolbartitle; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); getsupportactionbar().setdisplayshowtitleenabled(false); typeface mytypeface = typeface.createfromasset(getassets(), "fonts/panpizza.ttf"); toolbartitle = (textview) findviewbyid(r.id.toolbar_title); toolbartitle.settypeface(mytypeface); alertdialog.builder builder = new alertdialog.builder(getapplicationcontext()); builder.settitle(r.string.filter_title); builder.setmessage("test"); builder.setpositivebutton(r.string.ok_button, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int id) { dialog.dismiss(); } }); builder.setnegativebutton(r.string.cancel_button, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int id) { dialog.dismiss(); } }); alertdialog dialog = builder.create(); dialog.show(); } }
basically activity using toolbar (which replaces action bar) need use style activity has no action bar theme.appcompat.light.noactionbar
. if have own style dialog need inherit proper appcompat theme.
<style name="mydialog" parent="theme.appcompat.dialog"> <item name="android:windownotitle">true</item> ... </style>
alertdialog.builder builder = new alertdialog.builder(new contextthemewrapper(this, r.style.mydialog));
Comments
Post a Comment