Nullpointer异常在为EditText背景加载可绘制的xml时

时间:2014-09-09 06:42:34

标签: android background android-edittext drawable

我在EditText背景中使用Drawable xml。但它给出了空指针异常。 我使用以下内容:

我的布局xml:

    <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:background="#000000" >


    <EditText android:id="@+id/edtbox"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:singleLine="true"
        android:padding="10sp"
        android:imeOptions="actionNext"
       android:inputType="number"
       android:layout_marginTop="10dp"
       android:background="@drawable/edtbackground"
       />

     <EditText android:id="@+id/edtbox1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:singleLine="true"
        android:padding="10sp"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/edtbox"
        android:inputType="number"
       />

</RelativeLayout>

edtbackground.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
  <item>
      <shape android:shape="line" android:thickness="1dp">
      <solid android:color="#000000" />
      </shape>
  </item>

  <!-- main color -->
  <item android:bottom="1.5dp"
      android:left="1.5dp"
      android:right="1.5dp">
      <shape >
          <solid android:color="#FF0000" />
      </shape>
  </item>

  <!-- draw another block to cut-off the left and right bars -->
  <item android:bottom="2sp">
      <shape >
          <solid android:color="#000000" />
      </shape>
  </item>

  <Item   android:state_window_focused = "false">
       <shape >
          <solid android:color="#ffffff" />
      </shape>
  </Item>  
  <Item   android:state_focused = "true" >  
       <shape >
          <solid android:color="#FF0000" />
      </shape>
      </Item>


  </layer-list>

例外:

    09-09 11:51:27.711: E/AndroidRuntime(30677): FATAL EXCEPTION: main
09-09 11:51:27.711: E/AndroidRuntime(30677): 
java.lang.NullPointerException
09-09 11:51:27.711: 
E/AndroidRuntime(30677):    at android.view.GLES20Canvas.setupModifiers(GLES20Canvas.java:1413)
09-09 11:51:27.711: 
E/AndroidRuntime(30677):    at android.view.GLES20Canvas.drawLines(GLES20Canvas.java:994)
09-09 11:51:27.711: 
E/AndroidRuntime(30677):    at android.view.GLES20RecordingCanvas.drawLines(GLES20RecordingCanvas.java:161)
09-09 11:51:27.711: 
E/AndroidRuntime(30677):    at android.view.GLES20Canvas.drawLine(GLES20Canvas.java:984)
09-09 11:51:27.711: 
E/AndroidRuntime(30677):    at android.view.GLES20RecordingCanvas.drawLine(GLES20RecordingCanvas.java:155)
09-09 11:51:27.711: 
E/AndroidRuntime(30677):    at android.graphics.drawable.GradientDrawable.draw(GradientDrawable.java:537)
09-09 11:51:27.711: 
E/AndroidRuntime(30677):    at android.graphics.drawable.LayerDrawable.draw(LayerDrawable.java:345)

所以,请指导我做了什么错误。

1 个答案:

答案 0 :(得分:1)

<Item android:state_window_focused = "false">
    <shape>
        <solid android:color="#ffffff" />
    </shape>
</Item>  
<Item android:state_focused = "true" >  
    <shape>
        <solid android:color="#FF0000" />
    </shape>
</Item>

Item不是正确的令牌。将这些更改为item,并使用小写i

<强> ASIDE

即使进行了上述更改,您的可绘制XML也可能不会按预期方式运行。特别是:

  • 属性android:bottomandroid:leftandroid:right不适用于<item>内的<layer-list>元素。如果这些是填充,那么您需要在<padding>元素内使用<shape>元素。如果这些是插入内容,则需要在<inset>元素中使用<item>元素。
  • 属性android:state_window_focusedandroid:state_focused不适用于<item>内的<layer-list>元素。它们用于item元素内的selector元素。

请参阅this documentation并检查您使用的属性是否适用于相应的元素。此页面上有很多示例。我不知道当在这些元素上使用不适用的属性时会发生什么;我认为他们被忽略了,但你永远不会知道。