Ishan hrx
Ishan hrx

Reputation: 423

Resources$NotFoundException on Android 4.4

I am using vector in ImageView in my activity, the app works just fine on android 7.0 but crashes on android 4.4. Logcat says, Resources$NotFoundException. I have tried solutions posted here on stackoverflow but none of them seems to be working.

These are the solutions I tried.

  1. added this in my gradle file

    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
    
  2. Added this in OnCreate of activity

    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); 
    
  3. Replaced android:src with app:srcCompat in XML file.

This is my code in XML

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_arrow_back_white_24dp"
            android:tint="@color/colorPrimary"
            android:id="@+id/details_back"
            android:layout_margin="15dp"
            android:layout_alignParentStart="true"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/share"
            android:tint="@color/colorPrimary"
            android:id="@+id/details_share"
            android:layout_margin="15dp"
            android:layout_alignParentEnd="true"/>

    </RelativeLayout>

this is the code in JAVA file

    ImageView detail_share;
    detail_share = (ImageView) findViewById(R.id.details_share);
    detail_share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // code for sharing item

        }
    });

Also, the error occurs in second ImageView, not the first one, despite of being exactly same. I am treating both images exactly same in java file too.

Upvotes: 1

Views: 117

Answers (1)

Chirag
Chirag

Reputation: 478

I had same issue a few days back. Android 4.4 does not support vectors with (v21) written in faded font after their names in android studio directory tree, these vector graphics are only supported by API 21+. Ideally Android studio should split the vector into PNG files with different sizes but for some unclear reason Android studio does not do it on it's own sometimes.

Looks like you are trying to use the vector graphic for a "Share" icon in the above described scenario, try importing the share icon from Material icons in Android Studio only, this way the vectors imported are supported by Android 4.4 too.

Upvotes: 1

Related Questions