在 Android 10 上崩溃(布局/abc_screen_simple行 #17 中的膨胀异常)



我的应用程序从Android 4.3到Android 9 Pie运行良好,但我的应用程序在Android 10(Q API 29)上不起作用并崩溃。这是我的日志猫 - 为什么会发生这种情况?

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{ir.mahdi.circulars/ir.mahdi.circulars.MainActivity}: 
android.view.InflateException: Binary XML file line #17 
in ir.mahdi.circulars:layout/abc_screen_simple: Binary XML file line #17 
in ir.mahdi.circulars:layout/abc_screen_simple: 
Error inflating class androidx.appcompat.widget.FitWindowsLinearLayout

这是我的主要活动.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:layoutDirection="ltr"
tools:context=".MainActivity">

</androidx.coordinatorlayout.widget.CoordinatorLayout>

更新

apply plugin: 'com.android.application'
android {
compileSdkVersion 29
defaultConfig {
minSdkVersion 16
targetSdkVersion 29
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
} }
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' }

Calligraphy更新到最新版本以解决此问题: 链接: https://github.com/InflationX/Calligraphy/issues/35

更具体地说,书法ViewPump都需要更新:

implementation 'io.github.inflationx:calligraphy3:3.1.1'
implementation 'io.github.inflationx:viewpump:2.0.3'

从书法2 迁移到 3 需要更改一些代码;请参阅书法 3 自述文件中的示例。

您需要根据新版本更新书法版本并更改代码

您需要将依赖项中的存储库从

implementation "uk.co.chrisjenx:calligraphy:$caligraphyVersion"

implementation 'io.github.inflationx:calligraphy3:3.1.1'
implementation 'io.github.inflationx:viewpump:2.0.3'

您需要更改导入的用法

import uk.co.chrisjenx.calligraphy.CalligraphyConfig;

import io.github.inflationx.calligraphy3.CalligraphyConfig;

书法配置来自

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath(getResources().getString(R.string.bariol))
.setFontAttrId(R.attr.fontPath)
.build())) 
.build());

ViewPump.init(ViewPump.builder()
.addInterceptor(new CalligraphyInterceptor(
new CalligraphyConfig.Builder()

.setDefaultFontPath(getResources().getString(R.string.bariol))
.setFontAttrId(R.attr.fontPath)
.build()))
.build());

我使用了字体bariol,您可以将其更改为您的字体。

&newbase 至

super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase));
Please follow this below steps.
1. First of all check build.gradle(:app) file
2. If you are using  this below library:
implementation 'uk.co.chrisjenx:calligraphy:2.3.0'
3. Update this " implementation 'uk.co.chrisjenx:calligraphy:2.3.0' " library with this below library.
implementation 'io.github.inflationx:viewpump:2.0.3'
implementation 'io.github.inflationx:calligraphy3:3.1.1'
4. In project where You use this below code :
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

5. Update it with this below code:

@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase));
}

毫无疑问,您的问题是书法图书馆,我遇到了同样的问题,有两种方法可以解决它:

  1. 返回到目标版本 28,并等待可能的更新 书法图书馆。阿拉伯数字
  2. 删除库

关于例外:

书法中的异常错误来自库中反射的使用。请参阅此异常的最后一行:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example, PID: 8220
android.view.InflateException: Binary XML file line #17 in com.example:layout/abc_screen_simple: Binary XML file line #17 in com.example/abc_screen_simple: Error inflating class androidx.appcompat.widget.FitWindowsLinearLayout
Caused by: android.view.InflateException: Binary XML file line #17 in com.example/abc_screen_simple: Error inflating class androidx.appcompat.widget.FitWindowsLinearLayout
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.reflect.Field.get(java.lang.Object)' on a null object reference

Android 在您的文档中解释说,某些反射方法(非 SDK 接口)在 API 29 平台中受到限制。

通过 Class.getDeclaredField() 或 Class.getField() --------> NoSuchFieldException 引发的反射

Reflection via Class.getDeclaredMethod(), Class.getMethod() ----> NoSuchMethodException thrown.

通过 Class.getDeclaredFields()、Class.getFields() --------> 不在结果中的非 SDK 成员进行反射。

通过 Class.getDeclaredMethods(), Class.getMethods() 反射 -> 非 SDK 成员不在结果中

来源:非 SDK 接口限制

如果您使用的是书法,则应迁移到书法3: https://github.com/InflationX/Calligraphy

implementation 'io.github.inflationx:calligraphy3:3.1.1'
implementation 'io.github.inflationx:viewpump:2.0.3'

迁移自 chrisjenx/Calligraphy 到通货膨胀X/书法 并将Calligraphy更新到最新版本

您可以将 buildTools 版本和 sdk 版本从 29 更改为 28

targetSdk = 28
compileSdk = 28
buildTools = '28.0.3'

如果 targetSdkVersion等于29或更高,则此依赖项implementation 'com.ice.restring:restring:1.0.0'也会导致此崩溃,

因此,如果您的 gradle 中有此依赖项 (implementation 'com.ice.restring:restring:1.0.0'),您可以通过删除它或使用另一个与targetSdkVersion 29一起使用的库来解决此问题

遇到此问题时,只需将应用程序中的书法和视图泵更新到最新版本即可。目前,当前版本为: 实现 'io.github.inflationx:calligraphy3:3.1.1' 实现 'io.github.inflationx:viewpump:2.0.3'

build.gradle(:app)文件中更新您的书法导入,如下所示

实现 'io.github.inflationx:viewpump:2.0.3'

实现 'io.github.inflationx:calligraphy3:3.1.1'

并使用ViewPumpContextWrapper而不是CalligraphyContextWrapper

最新更新