91

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

10 回答 10

133

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

更具体地说,CalligraphyViewPump 都需要更新:

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

从 Calligraphy 2 迁移到 3 需要一些代码更改;请参阅Calligraphy 3 README中的示例。

于 2019-08-12T13:57:12.710 回答
59

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

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

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));
于 2020-08-07T09:51:11.327 回答
20
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));
    }
于 2020-04-07T09:12:04.327 回答
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 接口的限制

于 2019-07-27T12:17:08.733 回答
8

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

implementation 'io.github.inflationx:calligraphy3:3.1.1'
implementation 'io.github.inflationx:viewpump:2.0.3'
于 2019-10-26T11:32:50.310 回答
4

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

于 2019-11-26T03:14:43.967 回答
2

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

    targetSdk = 28
    compileSdk = 28
    buildTools = '28.0.3'
于 2019-06-23T13:20:09.057 回答
2

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

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

于 2020-12-31T12:04:23.090 回答
1

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

于 2019-10-17T13:51:01.160 回答
0

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

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

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

并使用ViewPumpContextWrapper而不是CalligraphyContextWrapper

于 2020-11-25T11:19:21.117 回答