I get many weird reports of app crashes (using app compat library) when OS calls the getView() of one of my adapter. It crashes in getDrawable() with ResourceNotFoundException under Android 4.x or RuntimeException under Android 5.x or UnsupportedOperationException under Android 6.x. The getView() is actually calling inflate() with a mostly simple layout file.
The call stack is always like this:
at android.content.res.TypedArray.getDrawable (TypedArray.java)
at android.view.View.<init> (View.java:3756)
at android.view.ViewGroup.<init> (ViewGroup.java)
at android.widget.LinearLayout.<init> (LinearLayout.java)
at android.widget.LinearLayout.<init> (LinearLayout.java)
at android.widget.LinearLayout.<init> (LinearLayout.java)
at java.lang.reflect.Constructor.newInstance (Constructor.java)
at java.lang.reflect.Constructor.newInstance (Constructor.java)
at android.view.LayoutInflater.createView (LayoutInflater.java)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView (PhoneLayoutInflater.java)
at android.view.LayoutInflater.onCreateView (LayoutInflater.java)
at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java)
at android.view.LayoutInflater.inflate (LayoutInflater.java)
at android.view.LayoutInflater.inflate (LayoutInflater.java)
at android.view.LayoutInflater.inflate (LayoutInflater.java)
at my_app.activity$adapter.getView
Here is the layout files that 'cause' this on Android 4.x and 5.x:
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"/>
<TextView
android:id="@+id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</LinearLayout>
<ImageButton
android:id="@+id/menu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#00000000"
android:src="@drawable/ic_menu_moreoverflow_normal_holo_dark"/>
The only drawable used in that layout is available in hdpi, mdpi and xhdpi. How come Android cannot find it? By the way, I can't reproduce this in emulator or an actual device running 4.x or 5.x!
Any suggestions will be appreciated, Those are my top 4 app crashes representing more than 90% of those!
I used to have background set to @null and changed it to #00000000 to no avail. Also included the drawable in theme's attributes and having src="?menu_overflow" but it didn't change anything.
Try
app:srcCompat="@drawable/ic_menu_moreoverflow_normal_holo_dark"
instead of
android:src="@drawable/ic_menu_moreoverflow_normal_holo_dark" // in your image button
Update
Dont forget to use android's android.support.v7.widget.AppCompatImageButton
instead of ImageButton
(this wont crash ).
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/menu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#00000000"
app:srcCompat="@drawable/ic_menu_moreoverflow_normal_holo_dark"/>