2016-12-29 71 views
0

我想將漸變背景放到textView。但是,當我運行它,我得到一個錯誤說:如何設置漸變作爲textview的背景?

java.lang.RuntimeException: Unable to start activity ComponentInfo{cresol.deal/cresol.deal.MainActivity}: android.view.InflateException: Binary XML file line #41: Error inflating class TextView 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
                  at android.app.ActivityThread.access$800(ActivityThread.java:155) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5343) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 
                  Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class TextView 
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288) 
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143) 
                  at cresol.deal.MainActivity.onCreate(MainActivity.java:15) 
                  at android.app.Activity.performCreate(Activity.java:6010) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)  
                  at android.app.ActivityThread.access$800(ActivityThread.java:155)  
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)  
                  at android.os.Handler.dispatchMessage(Handler.java:102)  
                  at android.os.Looper.loop(Looper.java:135)  
                  at android.app.ActivityThread.main(ActivityThread.java:5343)  
                  at java.lang.reflect.Method.invoke(Native Method)  
                  at java.lang.reflect.Method.invoke(Method.java:372)  
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)  
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)  
                  Caused by: android.content.res.Resources$NotFoundException: File res/drawable/main_pink_gradient.xml from drawable resource ID #0x7f020057 
                  at android.content.res.Resources.loadDrawableForCookie(Resources.java:2482) 
                  at android.content.res.Resources.loadDrawable(Resources.java:2384) 
                  at android.content.res.TypedArray.getDrawable(TypedArray.java:749) 
                  at android.view.View.<init>(View.java:3742) 
                  at android.widget.TextView.<init>(TextView.java:637) 
                  at android.widget.TextView.<init>(TextView.java:632) 
                  at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:62) 
                  at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:58) 
                  at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103) 
                  at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1021) 
                  at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1080) 
                  at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47) 
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)  
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:504)  
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:414)  
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:365)  
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288)  
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)  
                  at cresol.deal.MainActivity.onCreate(MainActivity.java:15)  
                  at android.app.Activity.performCreate(Activity.java:6010)  
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)  
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)  
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)  
                  at android.app.ActivityThread.access$800(ActivityThread.java:155)  
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)  
                  at android.os.Handler.dispatchMessage(Handler.java:102)  
                  at android.os.Looper.loop(Looper.java:135)  
                  at android.app.ActivityThread.main(ActivityThread.java:5343)  
                  at java.lang.reflect.Method.invoke(Native Method)  
                  at java.lang.reflect.Method.invoke(Method.java:372)  
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)  
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)  
                  Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #5<gradient> tag requires 'angle' attribute to be a multiple of 45 
                  at android.graphics.drawable.GradientDrawable.updateGradientDrawableGradient(GradientDrawable.java:1354) 
                  at android.graphics.drawable.GradientDrawable.inflateChildElements(GradientDrawable.java:1176) 
                  at android.graphics.drawable.GradientDrawable.inflate(GradientDrawable.java:1019) 
                  at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1128) 
                  at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:199) 
                  at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:127) 
                  at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1128) 
                  at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1032) 
                  at android.content.res.Resources.loadDrawableForCookie(Resources.java:2472) 
                  at android.content.res.Resources.loadDrawable(Resources.java:2384)  
                  at android.content.res.TypedArray.getDrawable(TypedArray.java:749)  
                  at android.view.View.<init>(View.java:3742)  
                  at android.widget.TextView.<init>(TextView.java:637)  
                  at android.widget.TextView.<init>(TextView.java:632)  
                  at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:62)  
                  at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:58)  
                  at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)  
                  at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1021)  
                  at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1080)  
                  at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)  
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)  
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)  
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:504)  
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:414)  
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:365)  
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288)  
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)  
                  at cresol.deal.MainActivity.onCreate(MainActivity.java:15)  
                  at android.app.Activity.performCreate(Activity.java:6010)  
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)  
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)  
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)  
                  at android.app.ActivityThread.access$800(ActivityThread.java:155)  
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)  
                  at android.os.Handler.dispatchMessage(Handler.java:102)  
                  at android.os.Looper.loop(Looper.java:135)  
                  at android.app.ActivityThread.main(ActivityThread.java:5343)  
                  at java.lang.reflect.Method.invoke(Native Method)  
                  at java.lang.reflect.Method.invoke(Method.java:372)  
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)  
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)  

我做了一個XML文件,下面的代碼:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item> 
    <shape> 
    <gradient 
     android:startColor="@color/mainPinkStart" 
     android:endColor="@color/mainPinkEnd" 
     android:angle="125" /> 
    </shape> 
</item> 
</selector> 

,我的主要活動代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="cresol.deal.MainActivity"> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:orientation="vertical"> 
<HorizontalScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:overScrollMode="never" 
    android:scrollbars="none" 
    android:id="@+id/horizontalScrollView"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp" 
     android:orientation="horizontal" 

     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true"> 

     <ImageButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/home_unpressed" 
      android:background="@null" 
      android:id="@+id/main_imageButton_home" 
      /> 

    </LinearLayout> 
</HorizontalScrollView> 
</LinearLayout> 
<TextView 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:background="@drawable/main_pink_gradient"/> 

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    card_view:cardCornerRadius="20dp" 
    card_view:cardElevation="8dp" 
    card_view:cardMaxElevation="10dp" 
    card_view:cardBackgroundColor="#fff" 
    android:background="#a9a9a9" 
    android:layout_marginLeft="20dp" 
    android:layout_marginRight="20dp" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="30dp"> 

</android.support.v7.widget.CardView> 
</RelativeLayout> 

我的顏色文件:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<color name="colorPrimary">#3F51B5</color> 
<color name="colorPrimaryDark">#303F9F</color> 
<color name="colorAccent">#FF4081</color> 
<color name="mainPinkStart">#F44195</color> 
<color name="mainPinkEnd">#EE82FA</color> 
</resources> 

我找不到這個地方正在發生的任何問題.... 我使用這個XML文件也試過:

<shape 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<gradient 
    android:startColor="@color/mainPinkStart" 
    android:endColor="@color/mainPinkEnd" 
    android:angle="125" /> 
</shape> 

但是這也沒有工作,我得到了同樣的錯誤。

+1

請通過日誌。問題已在日誌中指定。 – sJy

+0

可能重複的[Android資源未找到異常?](http://stackoverflow.com/questions/7727808/android-resource-not-found-exception) –

+0

是的,它說inflaterexception ..該XML文件中的哪一部分是錯誤的這使得它成爲一個例外...我所採用的代碼只是在StackOverflow問題中工作並回答了問題。 –

回答

0
android:angle="125" 

125不是45多個,45

支持角度的使用多個45,90,135,180,225,270,315,360

+0

偉大..完美...非常感謝你...你們是該死的知識淵博 –

0

梯度標籤需要 '角度' 屬性是的倍數45

<shape 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<gradient 
android:startColor="@color/mainPinkStart" 
android:endColor="@color/mainPinkEnd" 
android:angle="90" /> 
</shape> 
+0

你真的是知識淵博,你有這麼多的小事情關於這個。令人欽佩 –