簡體   English   中英

Android 4.4 上的 Resources$NotFoundException

[英]Resources$NotFoundException on Android 4.4

我在我的活動中使用 ImageView 中的矢量,該應用程序在 android 7.0 上運行良好,但在 android 4.4 上崩潰。 Logcat 說, Resources$NotFoundException 我已經嘗試過在 stackoverflow 上發布的解決方案,但它們似乎都不起作用。

這些是我嘗試過的解決方案。

  1. 在我的 gradle 文件中添加了這個

    defaultConfig { vectorDrawables.useSupportLibrary = true }
  2. 在活動的 OnCreate 中添加了這個

    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
  3. 在 XML 文件中用app:srcCompat替換了android:src

這是我的 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>

這是JAVA文件中的代碼

    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

        }
    });

此外,錯誤發生在第二個 ImageView 中,而不是第一個,盡管完全相同。 我也在 java 文件中處理兩個圖像完全相同。

幾天前我遇到了同樣的問題。 Android 4.4 不支持在 android studio 目錄樹中以褪色字體書寫的矢量 (v21),這些矢量圖形僅支持 API 21+。 理想情況下,Android Studio 應該將矢量拆分為不同大小的 PNG 文件,但由於某些不清楚的原因,Android Studio 有時不會自行完成。

看起來您正在嘗試在上述場景中使用矢量圖形作為“共享”圖標,請嘗試僅從 Android Studio 中的 Material 圖標導入共享圖標,這樣導入的向量也受 Android 4.4 支持。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM