'Android App crashes on older devices
My app works perfectly on my phone. On some older devices it crashes though:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blub.bla/com.blub.bla.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class ImageButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2572)
...
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class ImageButton
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at
...
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_google_plus_icon.xml from drawable resource ID #0x7f0700ab
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2872)
...
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #0: invalid color state list tag gradient
at android.content.res.ColorStateList.createFromXmlInner(ColorStateList.java:217)
at
...
I checked other posts and most of the time it had something to do with gradle, and I can say that i have vectorDrawables.useSupportLibrary = true
set.
Gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
repositories {
mavenCentral()
}
defaultConfig {
applicationId "com.blub.bla"
vectorDrawables.useSupportLibrary = true
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
android {
lintOptions {
disable 'RestrictedApi'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.facebook.android:facebook-login:[4,5)'
implementation 'com.facebook.android:facebook-share:[4,5)'
implementation 'com.android.support:appcompat-v7:27.0.1'
implementation 'com.android.support:design:27.0.1'
implementation 'com.android.support:support-v4:27.0.1'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.squareup.okhttp:okhttp:2.4.0'
implementation 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
implementation 'com.google.android.gms:play-services-location:10.+'
implementation 'com.google.android.gms:play-services-places:10.+'
implementation 'com.google.maps.android:android-maps-utils:0.4+'
implementation 'eu.the4thfloor.volley:com.android.volley:2015.05.28'
testImplementation 'junit:junit:4.12'
}
Edit: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@drawable/bg"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:itemIconTint="@color/white"
app:itemTextColor="@color/white"
app:menu="@menu/activity_main_drawer">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
>
<com.facebook.share.widget.ShareButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:id="@+id/fb_share_button"/>
<ImageButton
android:id="@+id/sm_twitter"
android:layout_width="30dp"
android:layout_height="30dp"
android:scaleType="fitXY"
app:srcCompat="@drawable/ic_twitter_bird_logo"
android:background="@color/transparent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
<ImageButton
android:id="@+id/sm_facebook"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:scaleType="fitXY"
app:srcCompat="@drawable/ic_f_icon_color"
android:background="@color/transparent"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@id/sm_twitter"
/>
<ImageButton
android:id="@+id/sm_gplus"
android:layout_width="30dp"
android:layout_height="30dp"
android:scaleType="fitXY"
app:srcCompat="@drawable/ic_google_plus_icon"
android:background="@color/transparent"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@id/sm_facebook"
/>
</RelativeLayout >
</RelativeLayout>
</android.support.design.widget.NavigationView>>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="@drawable/bg"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_filter"
app:itemIconTint="@color/white"
app:itemTextColor="@color/white">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="175dp"
android:orientation="horizontal">
<include
layout="@layout/filter_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Does anyone know how to fix that? :(
Solution 1:[1]
First Problem: I had some resources in drawable/v21
instead of drawable
(not easy to see, since it's showing the same folder in Android studio, just with a (21)
next to the file name
Second Problem: Use app:srcCompat
instead of android:background
or android:res
Solution 2:[2]
You can't use gradients in vector drawables until 24 API.
About android:fillColor
, for example, from here https://developer.android.com/reference/android/graphics/drawable/VectorDrawable.html:
Specifies the color used to fill the path. May be a color or, for SDK 24+, a color state list or a gradient color
Solution 3:[3]
That happens when you try to show an SVG in an Android version under 21, you can use AppCompatImageButton instead of ImageButton or AppCompatImageView with app: srcCompat
Solution 4:[4]
For providing a general solution, IMO (and in my case), older versions are more strict about checking null data,resources,etc... and newer api versions seem more tolerant in contrast. Here is a good article about how to deal with null values. Avoid Check for Null Statement in Java
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Lisa Cleaver |
Solution 2 | geiger |
Solution 3 | Woz |
Solution 4 | Tanzer |