问题
My application works fine from Android 4.3 until Android 9 Pie, but my application doesn't work on Android 10 (Q API 29) and crashes. This is my logcat - why this is happening?
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
and this is my 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>
update
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' }
回答1:
Update Calligraphy
to newest version to solve this problem:
Link: https://github.com/InflationX/Calligraphy/issues/35
More specifically, both Calligraphy and ViewPump need to be updated:
implementation 'io.github.inflationx:calligraphy3:3.1.1'
implementation 'io.github.inflationx:viewpump:2.0.3'
Migrating from Calligraphy 2 to 3 requires some code changes; see examples in Calligraphy 3 README.
回答2:
if you are using calligraphy so you should migrate to calligraphy3 : https://github.com/InflationX/Calligraphy
implementation 'io.github.inflationx:calligraphy3:3.1.1'
implementation 'io.github.inflationx:viewpump:2.0.3'
回答3:
Definitively your problem is the Calligraphy library, I've got the same problem and there are 2 ways to solve it:
- Go back to target version 28, and wait a possible update of the Calligraphy library. 2
- Remove the library
About the exception:
The exception error in Calligraphy comes from the use of the reflection in the library. See the last line of this exception:
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 in your documentation explain that some reflection methods (non-SDK interfaces) are restricted in the API 29 platform.
Reflection via Class.getDeclaredField() or Class.getField() --------> NoSuchFieldException thrown
Reflection via Class.getDeclaredMethod(), Class.getMethod() ----> NoSuchMethodException thrown.
Reflection via Class.getDeclaredFields(), Class.getFields() --------> Non-SDK members not in results.
Reflection via Class.getDeclaredMethods(), Class.getMethods() -> Non-SDK members not in results
Source: Restrictions on non-SDK interfaces
回答4:
You can change buildTools version and sdk version from 29 to 28
targetSdk = 28
compileSdk = 28
buildTools = '28.0.3'
回答5:
When faced with this issue, just update the calligraphy and viewpump in your app gradle to the latest version. Presently, the current version is: implementation 'io.github.inflationx:calligraphy3:3.1.1' implementation 'io.github.inflationx:viewpump:2.0.3'
回答6:
Migrate from chrisjenx/Calligraphy
to InflationX/Calligraphy
and update Calligraphy
to newest version
来源:https://stackoverflow.com/questions/56524914/crash-on-android-10-inflateexception-in-layout-abc-screen-simple-line-17