Android 10 上崩溃(layout/abc_screen_simple 第 17 行中的 InflateException)

问题描述 投票:0回答:10

我的应用程序从 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' }
android androidx android-10.0 calligraphy
10个回答
136
投票

更新

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

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

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

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


67
投票

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

您需要更改依赖项中的存储库

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));

20
投票
 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));
        }

9
投票

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

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

通过 Class.getDeclaredField() 或 Class.getField() 进行反射 --------> 抛出 NoSuchFieldException

通过 Class.getDeclaredMethod()、Class.getMethod() 进行反射 ----> 抛出 NoSuchMethodException。

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

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

来源:非SDK接口限制


8
投票

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

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

4
投票

chrisjenx/Calligraphy 迁移InflationX/书法 并将

Calligraphy
更新到最新版本


2
投票

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

    targetSdk = 28
    compileSdk = 28
    buildTools = '28.0.3'

2
投票

此依赖项

implementation 'com.ice.restring:restring:1.0.0'
也会导致此崩溃iftargetSdkVersion等于29或更高,

因此,如果您的 gradle 中有此依赖项(

implementation 'com.ice.restring:restring:1.0.0'
),您可以通过删除它或使用另一个与
targetSdkVersion 29

配合使用的库来解决此问题

1
投票

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


0
投票

使用以下更新build.gradle(:app)文件中的calligraphy

导入

实现“io.github.inflationx:viewpump:2.0.3”

实现“io.github.inflationx:calligraphy3:3.1.1”

并使用 ViewPumpContextWrapper 而不是 CalligraphyContextWrapper

© www.soinside.com 2019 - 2024. All rights reserved.