我正在尝试将渐变背景添加到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>
但这也没有奏效,我也遇到了同样的错误。
答案 0 :(得分:0)
android:angle="125"
125不是45的倍数,使用45的倍数
支撑角为45,90,135,180,225,270,315,360
答案 1 :(得分:0)
渐变标记要求'angle'属性为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>