androidbackgrounddrawableandroid-relativelayoutandroid-inflate

android relative layout background crash


Well' I have a RelativeLayout

    <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="wrap_content"
    android:clickable="true"
    android:background="@drawable/my_shape_normal"
    android:onClick="startTestTillError">

and my_shape_normal is defined as

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:startColor="#FFFFFF00"
    android:endColor="#80FFFFFF"
    android:angle="120"/>
<padding android:left="7dp"
    android:top="7dp"
    android:right="7dp"
    android:bottom="7dp" />
<corners android:radius="8dp" />
</shape>

On loading an exception is raised: android.view.InflateException at the RelativeLayout's line. If I remove "android:background", everything works perfect. What's wrong with my_shape_normal?


Solution

  • When I cut & paste your XML my LogCat reads:

    Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #6
        <gradient> tag requires 'angle' attribute to be a multiple of 45
    

    So I've updated your shape XML to reflect this requirement:

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
        android:padding="7dp" >
    
        <gradient
            android:angle="135"
            android:endColor="#80FFFFFF"
            android:startColor="#FFFFFF00" />
    
        <corners android:radius="8dp" />
    
    </shape>
    

    Notice I also combined the identical padding elements into one attribute in shape.