2017-10-21 88 views
1

我在我的活動中使用ImageView中的矢量,該應用在Android 7.0上工作得很好,但在Android 4.4上崩潰。 Logcat說,Resources$NotFoundException。我已經嘗試過在這裏發佈stackoverflow解決方案,但他們似乎沒有工作。

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

  1. 在我gradle這個文件

    defaultConfig { 
        vectorDrawables.useSupportLibrary = true 
    } 
    
  2. 添加在XML文件中加入這個本在OnCreate中活動

    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); 
    
  3. 替換android:srcapp:srcCompat

這是我在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文件中處理兩個圖像完全相同。

回答

1

幾天前我有同樣的問題。 Android 4.4不支持在android studio目錄樹中名稱爲褪色字體(v21)的矢量,這些矢量圖形僅支持API 21+。理想情況下,Android工作室應該將矢量拆分爲不同大小的PNG文件,但由於某些不明的原因,Android工作室有時並不會自行完成。

看起來您正嘗試在上述場景中使用「共享」圖標的矢量圖形,請嘗試僅從Android Studio中的「材質」圖標導入共享圖標,這樣導入的矢量也受Android 4.4支持。

相關問題