Android 10 时崩溃(layout/abc_screen_simple 行 #17 中的 InflateException)

Crash on Android 10 (InflateException in layout/abc_screen_simple line #17)

我的应用程序从 Android 4.3 到 Android 9 Pie 运行良好,但我的应用程序在 Android 10 (Q API 29) 上无法运行并崩溃.这是我的 logcat - 为什么会这样?

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

这是我的 mainActivity.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' }

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

    targetSdk = 28
    compileSdk = 28
    buildTools = '28.0.3'

你的问题肯定是书法库,我也遇到了同样的问题,有两种方法可以解决:

  1. 返回目标版本 28,等待可能的更新 书法图书馆。 2
  2. 删除库

关于异常:

Calligraphy中的异常错误来自库中反射的使用。看到这个异常的最后一行:

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 平台中受到限制。

Reflection via Class.getDeclaredField() or Class.getField() --------> NoSuchFieldException thrown

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

Reflection via Class.getDeclaredFields(), Class.getFields() --------> Non-SDK members not in results.

Reflection via Class.getDeclaredMethods(), Class.getMethods() -> Non-SDK members not in results

Source: Restrictions on non-SDK interfaces

更新Calligraphy到最新版本来解决这个问题: Link: https://github.com/InflationX/Calligraphy/issues/35

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

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

从 Calligraphy 2 迁移到 3 需要更改一些代码;请参阅 Calligraphy 3 README.

中的示例

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

如果您正在使用书法,那么您应该迁移到书法 3: https://github.com/InflationX/Calligraphy

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

迁移自 chrisjenx/CalligraphyInflationX/Calligraphy 并将 Calligraphy 更新到最新版本

Please foloow 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));
    }

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

您需要从

更改依赖项中的存储库
implementation "uk.co.chrisjenx:calligraphy:$caligraphyVersion"

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

您需要更改 import from

的用法
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字体,你可以换成你的。

& 新基地到

super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase));

使用下面的更新书法build.gradle(:app)文件中的导入

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

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

并使用 ViewPumpContextWrapper 而不是 CalligraphyContextWrapper

这个依赖implementation 'com.ice.restring:restring:1.0.0'也会导致这个崩溃if targetSdkVersion等于29 或更高,

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