yanzhenjie / AndPermission

:strawberry: Permissions manager for Android platform.
https://yanzhenjie.com/AndPermission
Apache License 2.0
6.63k stars 1.1k forks source link

2.0.3 WaitDialog抛异常 #519

Open xbesung opened 5 years ago

xbesung commented 5 years ago

手机型号:华为 Mate 9 品牌系统版本:EMUI 9.1 Android系统版本: 9 targetSdkVersion:28 权限:Permission.RECORD_AUDIO

调用AndPermission.with(this).runtime().permission(Permission.Group.MICROPHONE).onGranted(...).start()就报异常了,看起来是因为WaitDialog继承了AppCompatDialog但是设置的style缺少了AppCompat必需的属性,我确认了一下androidx.appcompat的版本是1.1.0

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
    at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:696)
    at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:552)
    at androidx.appcompat.app.AppCompatDialog.setContentView(AppCompatDialog.java:95)
    at com.yanzhenjie.permission.task.WaitDialog.<init>(WaitDialog.java:32)
    at com.yanzhenjie.permission.task.TaskExecutor.<init>(TaskExecutor.java:35)
    at com.yanzhenjie.permission.runtime.MRequest$1.<init>(MRequest.java:101)
    at com.yanzhenjie.permission.runtime.MRequest.onCallback(MRequest.java:101)
    at com.yanzhenjie.permission.runtime.MRequest.start(MRequest.java:81)

AppCompatDelegateImpl.createSubDecor具体报异常的源码:

    TypedArray a = mContext.obtainStyledAttributes(R.styleable.AppCompatTheme);

    if (!a.hasValue(R.styleable.AppCompatTheme_windowActionBar)) {
        a.recycle();
        throw new IllegalStateException(
                "You need to use a Theme.AppCompat theme (or descendant) with this activity.");
    }
xbesung commented 5 years ago

我知道为什么了,因为我的this是个Service,所以应该是缺乏theme相关的信息,作为mSource传进去以后,再作为WaitDialog的初始化context就出错了。但是就算把WaitDialog改成Dialog也会有别的异常出来的,所以除了AppCompat应该也有其他地方不能接受Service作为context,看来基本上新版的只能用Activity传入context。 目前解决办法,回退到x:2.0.1。