【问题标题】:Crash on Android 10 (InflateException in layout/abc_screen_simple line #17)Android 10 崩溃(layout/abc_screen_simple 第 17 行中的 InflateException)
【发布时间】:2019-10-24 18:24:29
【问题描述】:

我的应用程序从 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' }

【问题讨论】:

  • 使用这个库 com.google.android.material:material:1.0.0-rc01 insted of com.google.android.material:material:1.0.0
  • @MilanPansuriya 不起作用
  • 请分享您的整个错误日志...通常,在这些情况下的真正原因会在崩溃跟踪的末尾提到
  • @W0rmH0le 我发现了问题,它来自书法库,所以我通过删除它来修复它
  • @git,除了删除Calligraphy,还有其他方法吗?我找不到该问题的解决方案,但发现这个库对字体很方便,知道吗?

标签: android androidx android-10.0 calligraphy


【解决方案1】:

更新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 需要更改一些代码;请参阅Calligraphy 3 README 中的示例。

【讨论】:

  • 这是正确答案。实现 'io.github.inflationx:calligraphy3:3.1.1' 实现 'io.github.inflationx:viewpump:2.0.3'
  • @KonstantinKuznetsov 是对的。我们案例中的问题:Calligraphy 库在 2.2.0 和 3.1.1 之间发生了很大变化,更新应用程序代码并非易事。例如,CalligraphyContextWrapper 在 3.1.1 中不再存在。
  • 更新:其实更新到Calligraphy 3还是很简单的; README 中的代码 sn-ps 使其简单明了。但问题是,至少在我们的例子中 Calligraphy 更新没有帮助:在那之后仍然会收到带有 layout/abc_screen_simple 的 InflateException。
  • @Jonik 嗯.. 很奇怪。也许您的应用程序中的一些独立模块仍然对 Calligraphy 有旧的依赖项?你有没有试过检查gradle中的传递依赖,也许你会看到一些奇怪的东西?
  • 我冒昧地将缺失的信息编辑成答案。
【解决方案2】:

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

您需要从

更改依赖项中的存储库
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 字体,你可以换成你的。

&newbase 到

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

【讨论】:

  • 感谢@Nafees Khabir 非常明确的解决方案。
【解决方案3】:
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));
    }

【讨论】:

  • 这个解决方案解释得当,它解决了问题
【解决方案4】:

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

  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 成员不在结果中

Source: Restrictions on non-SDK interfaces

【讨论】:

  • 很可能,在项目中添加了书法依赖项是有原因的。无需删除它;更新到 Calligraphy 3 还修复了 InflateException 问题:stackoverflow.com/a/57462616/56285
【解决方案5】:

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

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

【讨论】:

    【解决方案6】:

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

    【讨论】:

      【解决方案7】:

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

          targetSdk = 28
          compileSdk = 28
          buildTools = '28.0.3'
      

      【讨论】:

      • 这是一种解决方法,但在某些情况下,您无论如何都需要将targetSdk 更新为 29,并且您需要妥善处理该问题。
      • 只有targetSdk必须是28,compileSdk和buildTools可以是29,不过这是临时解决方案
      【解决方案8】:

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

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

      【讨论】:

        【解决方案9】:

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

        【讨论】:

          【解决方案10】:

          使用 below

          更新 build.gradle(:app) 文件中的 calligraphy 导入

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

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

          并使用 ViewPumpContextWrapper 而不是 CalligraphyContextWrapper

          【讨论】:

            猜你喜欢
            • 2015-02-21
            • 1970-01-01
            • 2020-04-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-12-14
            • 2020-07-28
            相关资源
            最近更新 更多