2017-08-09 26 views
0

我正在面對android.view.InflateException在我的應用程序中用於某些特定設備,在其他手機中它工作正常。但我不知道爲什麼。許多人說這個問題發生在可繪製的矢量圖像上。我在android:background= ""行中使用矢量圖像。是這樣做的InflateException是否有權通過在android:background中使用Vector drawable來製作android.view.InflateException?ImageView中的line:

<RelativeLayout 
    android:id="@+id/nameLayoutMain" 
    android:layout_width="wrap_content" 
    android:layout_height="120dp" 
    android:background="@drawable/shadow_164949"> 

    <ImageView 
     android:layout_width="80dp" 
     android:layout_height="400dp" 
     android:background="@drawable/draw_phone" 
     android:id="@+id/im1" 
     android:layout_alignParentTop="true" /> 

    <ImageView 
     android:layout_width="80dp" 
     android:layout_height="50dp" 
     app:srcCompat="@drawable/ic_phone" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_toLeftOf="@+id/linearLayoutName" 
     android:layout_toStartOf="@+id/linearLayoutName" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/linearLayoutName" 
     android:gravity="center_vertical" 
     android:layout_alignBottom="@+id/im1" 
     android:layout_toRightOf="@+id/im1" 
     android:layout_toEndOf="@+id/im1"> 

      <TextView 
       android:id="@+id/txtPhoneShow" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/my_phone_number" 
       android:textSize="18sp" 
       android:layout_alignParentTop="true" 
       android:layout_alignLeft="@+id/txtPhone" 
       android:layout_alignStart="@+id/txtPhone" /> 


      <TextView 
       android:id="@+id/txtPhone" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="01612548792" 
       android:textColor="@color/toolbar1" 
       android:textSize="20sp" 
       android:textStyle="bold" 
       android:layout_below="@+id/txtPhoneShow" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_marginLeft="20dp" 
       android:layout_marginStart="20dp" /> 

    </RelativeLayout> 

</RelativeLayout> 
+0

你能告訴我可繪製矢量的xml代碼嗎? –

+0

分享錯誤logcat .. – Omi

+0

你能否請檢查一下。它包含我的可繪製矢量圖像,我的XML,我的錯誤和我的Java代碼。 https://drive.google.com/open?id=0B7XpFVYwQlMYcHc3Q2lHWWU3cUE –

回答

0

在您的可繪製矢量xml中使用顏色代碼替換color.xml中的顏色名稱,如下所示;

<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:width="24dp" 
    android:height="24dp" 
    android:viewportWidth="24.0" 
    android:viewportHeight="24.0"> 
<path 
    android:fillColor="#4396e6" 
    android:pathData="M21,19V5c0,-1.1 -0.9,-2 -2,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2zM8.5,13.5l2.5,3.01L14.5,12l4.5,6H5l3.5,-4.5z"/> 

0

確保您已在app/build.gradle添加vectorDrawables.useSupportLibrary = truedefaultConfig

喜歡

defaultConfig { 
     vectorDrawables.useSupportLibrary = true 
     .... 
} 

EDIT 1 爲了支持vector在API小於21.還setContentView前在活動的onCreate方法添加此。

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); 
+0

是的,我在build.gradle –

+0

中加入了這一行@NazmusSaadat剛剛增加了一個。 –

相關問題