Android studio MIUI 11/12主题切换导致LifeCycleException、ClassCastException

Android studio MIUI 11/12主题切换导致LifeCycleException、ClassCastException,android-studio,classcastexception,android-dark-theme,miui,darkmode,Android Studio,Classcastexception,Android Dark Theme,Miui,Darkmode,由于MIUI11/12使用自己的自定义暗模式实现,因此使用下面的代码会导致ClassCastException 和活动生命周期异常,因为DefaultNightMode和LocalNightMode始终未指定,即-100而不是0或1 如果有人为小米设备专门为MIUI 11/12构建了一个解决方案。请帮我解决这个问题 使用的代码: AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); this.recreat

由于MIUI11/12使用自己的自定义暗模式实现,因此使用下面的代码会导致ClassCastException 和活动生命周期异常,因为DefaultNightMode和LocalNightMode始终未指定,即-100而不是0或1

如果有人为小米设备专门为MIUI 11/12构建了一个解决方案。请帮我解决这个问题

使用的代码:

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
this.recreate();
错误原因:

2020-10-25 07:04:43.626 9167-9167/com.xxxx.xxxxxx E/ActivityInjector: get life cycle exception
    java.lang.ClassCastException: android.os.BinderProxy cannot be cast to android.app.servertransaction.ClientTransaction

我自己解决了。我摆弄了一个星期后就做了

以下是如何:

--而不是为每个活动设置android:forceDarkAllowed=“false”

--只需转到values.xml并将其放在AppTheme中

        <item name="android:forceDarkAllowed">false</item>
false
它将要求您创建valuesv29.xml。创建它,并从所有活动中删除forceDarkAllowed=“false”


欢迎。:)

你看过这个吗?它说重新创建活动是不必要的。根据MIUI下的Android版本,这可能是问题所在。谢谢回复。我已经注意到了这一点,问题不在于Android,而在于上面的MIUI皮肤。该应用程序在股票Android(相同的api)上运行完全正常。