我的应用程序从 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' }
更新
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 自述文件中的示例。
您需要更新书法版本并根据新版本更改代码
您需要更改依赖项中的存储库
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));
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));
}
你的问题肯定是书法库,我也有同样的问题,有两种方法可以解决它:
关于例外:
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 成员
如果您正在使用书法,那么您应该迁移到书法3: https://github.com/InflationX/Calligraphy
implementation 'io.github.inflationx:calligraphy3:3.1.1'
implementation 'io.github.inflationx:viewpump:2.0.3'
从 chrisjenx/Calligraphy 迁移 到InflationX/书法 并将
Calligraphy
更新到最新版本
您可以将buildTools版本和sdk版本从29更改为28
targetSdk = 28
compileSdk = 28
buildTools = '28.0.3'
此依赖项
implementation 'com.ice.restring:restring:1.0.0'
也会导致此崩溃iftargetSdkVersion等于29或更高,
因此,如果您的 gradle 中有此依赖项(
implementation 'com.ice.restring:restring:1.0.0'
),您可以通过删除它或使用另一个与 targetSdkVersion 29
配合使用的库来解决此问题
遇到此问题时,只需将应用程序 gradle 中的书法和 viewpump 更新到最新版本即可。目前,当前版本是: 实现 'io.github.inflationx:calligraphy3:3.1.1' 实现 'io.github.inflationx:viewpump:2.0.3'
使用以下更新build.gradle(:app)文件中的calligraphy
导入实现“io.github.inflationx:viewpump:2.0.3”
实现“io.github.inflationx:calligraphy3:3.1.1”
并使用 ViewPumpContextWrapper 而不是 CalligraphyContextWrapper