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

git*_*it 21 android calligraphy androidx android-10.0

我的应用程序在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
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

更新

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' }
Run Code Online (Sandbox Code Playgroud)

小智 51

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

您需要从以下位置更改依赖项中的存储库

implementation "uk.co.chrisjenx:calligraphy:$caligraphyVersion"
Run Code Online (Sandbox Code Playgroud)

implementation 'io.github.inflationx:calligraphy3:3.1.1'
implementation 'io.github.inflationx:viewpump:2.0.3'
Run Code Online (Sandbox Code Playgroud)

您需要更改 import from 的使用

import uk.co.chrisjenx.calligraphy.CalligraphyConfig;
Run Code Online (Sandbox Code Playgroud)

import io.github.inflationx.calligraphy3.CalligraphyConfig;
Run Code Online (Sandbox Code Playgroud)

书法配置来自

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
               .setDefaultFontPath(getResources().getString(R.string.bariol))
               .setFontAttrId(R.attr.fontPath)
                .build())) 
                .build());
Run Code Online (Sandbox Code Playgroud)

ViewPump.init(ViewPump.builder()
            .addInterceptor(new CalligraphyInterceptor(
                    new CalligraphyConfig.Builder()
                            
            .setDefaultFontPath(getResources().getString(R.string.bariol))
                            .setFontAttrId(R.attr.fontPath)
                            .build()))
            .build());
Run Code Online (Sandbox Code Playgroud)

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

& 新基地到

super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase));
Run Code Online (Sandbox Code Playgroud)


MEG*_*IYA 18

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));
    }
Run Code Online (Sandbox Code Playgroud)


Adm*_*kka 17

更新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'
Run Code Online (Sandbox Code Playgroud)

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

  • 更新:实际上,更新到书法3并不容易;[自述文件](https://github.com/InflationX/Calligraphy)中的代码段使其变得简单明了。但是问题是,至少在我们的案例中,**书法更新没有帮助**:在此之后,仍会通过`layout / abc_screen_simple`获得InflateException。 (2认同)

pab*_*rca 7

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

  1. 返回目标版本 28,并等待 Calligraphy 库的可能更新。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
Run Code Online (Sandbox Code Playgroud)

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 接口的限制


san*_*adi 7

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

implementation 'io.github.inflationx:calligraphy3:3.1.1'
implementation 'io.github.inflationx:viewpump:2.0.3'
Run Code Online (Sandbox Code Playgroud)