0

My app can run on android 4 devices but when I change minSdkVersion from 25 to 16 I got following error:

...
java.lang.RuntimeException: Unable to start activity
...
Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class ImageView
...
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/tab_btn.xml from drawable resource ID #0x7f020066

Activity:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
}

Activity layout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@color/background">

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/tab_btn" />
</LinearLayout>

Selector xml(tab_btn.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/tab_btn_selected" />
<item android:drawable="@drawable/tab_btn_deselected" />
</selector>

Does android 4 support multistate imageViews?

undefined
  • 623
  • 7
  • 27
  • 1
    Is changing from `app:srcCompat` to `android:src` fixing that behavior? – azizbekian Apr 19 '17 at 08:54
  • Or — even better — using `AppCompatImageView` instead. – Michael Apr 19 '17 at 08:58
  • nope, with `android:src` the error still there – undefined Apr 19 '17 at 08:59
  • Are you using vector images? – DevKRos Apr 19 '17 at 08:59
  • I think this post is related to your problem http://stackoverflow.com/questions/4512981/android-view-inflateexception-binary-xml-file-line-12-error-inflating-class . Probably you have to check the pixel resolution in your drawables images are just the minimum necessary for your layout. – Johny Apr 19 '17 at 09:03
  • @Michael:using `AppCompatImageView` instead of `ImageView` throws `Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.AppCompatImageView" on path: DexPathList[[zip file "/data/app/net.rhyboo.com.selectortest-1.apk"],nativeLibraryDirectories=[/data/app-lib/net.rhyboo.com.selectortest-1, /vendor/lib, /system/lib]]` @DevKRos:Yes I'm using vector drawables and I'm sure they're fine since they run ok on api level 25 – undefined Apr 19 '17 at 09:04
  • @undefined check my answer .Let me know if it works – DevKRos Apr 19 '17 at 09:05
  • @DevKRos a) Again I can run my test project just by changing minSdkVersion from 16 to 23 so resources are fine.b) using `app:src` instead of `app:srcCompat` produces parsing error `Unexpected namespace prefix app found for tag ImageView` – undefined Apr 19 '17 at 09:13
  • use android:src instead of app:src – DevKRos Apr 19 '17 at 09:18
  • Sorry but i dont understand that your device app is android 4 but you run it with minSdkVersion 25 ?? – Phạm Lam Apr 19 '17 at 10:08
  • @undefined Okay i find two possible workaround 1>You make convert webp image fro m vector 2> Use Selectors programatically and set images by using ContextCompat.GetDrawable(this, Resource.Drawable.tab_btn); – DevKRos Apr 19 '17 at 10:11

8 Answers8

1

Use android:src="@drawable/tab_btn" instead of app:srcCompat

Once that is done, Clean or invalidate cache and restart the project and run it. Sometimes resource Id's will not be refreshed.

Harshitha
  • 104
  • 3
1

1) Are you really using vector? If you're not using vector, consider to use android:src.

For AppCompat users, we’ve decided to remove the functionality which let you use vector drawables from resources on pre-Lollipop devices due to issues found in the implementation in version 23.2.0/23.2.1. Using app:srcCompat and setImageResource() continues to work.

So, by right android version lower than lollipop cannot use this feature (only for vector). So you might need to set minSdkVersion to 23 instead of 16. You can refer to AndroidDevelopers for this.

2) If you are not using vector and you still getting the error, probably you misplaced the file. Here is the hint:

Caused by: android.content.res.Resources$NotFoundException

Which folder did you put tab_btn.xml?. You need to ensure that the file is in drawable folder then use android:src="@drawable/tab_btn" instead app:srcCompat="@drawable/tab_btn".

Amad Yus
  • 2,856
  • 1
  • 24
  • 32
  • Its vector drawable and its in the drawable foldder.Once again the project runs ok on android 5+.I can't set minSdkVersion to 23 as my aim to make support of android 4 devices – undefined Apr 19 '17 at 10:32
0

USE background:

       android:background="@drawable/tab_btn" 
instead     
       app:srcCompat="@drawable/tab_btn"

Or USE

    android:src 
DevKRos
  • 422
  • 2
  • 15
  • downvoting without explaining ? This is sure shot working code – DevKRos Apr 19 '17 at 09:07
  • `android:background` throws same error but with some new info: `Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector` – undefined Apr 19 '17 at 09:22
  • You sure u used namespace android instead of app: just as android:src ?instead of app:src – DevKRos Apr 19 '17 at 09:34
  • I think you are using vector drawble if yes then they must needed to be backward compatible have a look at here https://android-developers.googleblog.com/2016/02/android-support-library-232.html So in order to make it backward compatible please use vectorDrawables.useSupportLibrary = true – DevKRos Apr 19 '17 at 09:41
  • Yes I mentioned I'm using vector drawables above and I already have useSupportLibrary=true in the build.gradle – undefined Apr 19 '17 at 09:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142064/discussion-between-devkros-and-undefined). – DevKRos Apr 19 '17 at 10:12
  • I am using png but still getting this issue. – Maya Mohite Oct 05 '20 at 07:42
0

Solved by removing all selectors from ImageViews plus myImage.setImageResource(R.drawable.my_drawable);
Special thanks to @DevKRos

undefined
  • 623
  • 7
  • 27
0

If you would like use selector for view with VectorDrawables you need to add this:

static {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

to every Activity where you want to use VectorDrawables on devices with versions below Android 5. Without selector you could use:

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/some_vector_drawable_without_selector" />
Djek-Grif
  • 1,391
  • 18
  • 18
0

Use
android:src="@drawable/app_logo"

and still if it crashing then check out your drawable image is not in drawable-v24 folder,

move your image to drawable folder. (mostly we need to do this if image is large in size or high resolution image)

note: Set ic_launcher image as src and if it works then problem is in your image. May be it large in size or high resolution.

hope this help you.

kunal khedkar
  • 877
  • 8
  • 6
0

I had a drawable file and before it was inside of drawable-24 folder, I just moved it to drawable folder and inside my gradled I added:

vectorDrawables.useSupportLibrary true

And I used:

app:srcCompat="@drawable/ic_picture"
-2

A. Make sure tab_btn.xml, tab_btn_deselected and tab_btn_deselected are exist in your res/drawable/ folder.

B. Use attribute android:src instead of app:srcCompat

Try this:

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/tab_btn" />
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61