您的位置:首页 > 移动开发

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.

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需要手动打开悬浮窗权限, 可以引导到开启页面
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐