ApplicationContext AlertDialog IllegalStateException: You need to use a Theme.AppCompat theme
2017-08-29 19:04
429 查看
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
创建AlertDialog时, Context如果用Activity不会有任何问题, 如果用Application会抛出上面的异常.
第二个参数需要传一个style进去, 比如上面那个
不在Activity里面的弹窗只能用系统级别的弹窗. 需要申请权限
有些Rom需要手动打开悬浮窗权限, 可以引导到开启页面
new AlertDialog.Builder(context)
创建AlertDialog时, Context如果用Activity不会有任何问题, 如果用Application会抛出上面的异常.
解决方法如下:
Builder使用两参的构造器, Builder(@NonNull Context context, @StyleRes int themeResId)new AlertDialog.Builder(getApplicationContext, android.support.v7.appcompat.R.style.Theme_AppCompat_Light_Dialog_Alert)
第二个参数需要传一个style进去, 比如上面那个
不在Activity里面的弹窗只能用系统级别的弹窗. 需要申请权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext, android.support.v7.appcompat.R.style.Theme_AppCompat_Light_Dialog_Alert); builder.setTitle("Title"); ...... AlertDialog alertDialog = builder.create(); //需要声明 android.permission.SYSTEM_ALERT_WINDOW权限 alertDialog.getWindow().setType(LayoutParams.TYPE_SYSTEM_ALERT); alertDialog.show();
有些Rom需要手动打开悬浮窗权限, 可以引导到开启页面
相关文章推荐
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this a
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this a
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this a
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this a
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this a
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this a
- java.lang.IllegalStateException: You need to use a theme.appcompat theme (or descendant) with this activity
- android启动Dialog风格Activity报错:java.lang.IllegalStateException: You need to use a theme.appcompat theme
- java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with ...
- Java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the
- java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the
- [问题]第一行代码_生命周期_you need to use a theme.appcompat theme (or descendant) with this activity
- Android 开发问题集锦之:You need to use a Theme.AppCompat theme (or descendant) with this activity.
- Android开发~you need to use a theme.appcompat theme (or descendant) with this activity
- 使用AlertDialog报错 You need to use a Theme.AppCompat theme (or descendant) with this activity.之解决