gpt4 book ai didi

android - 为 EditText 背景加载可绘制 xml 时出现空指针异常

转载 作者:行者123 更新时间:2023-11-30 02:41:07 24 4
gpt4 key购买 nike

我正在为 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)

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

最佳答案

<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 .

放在一边

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

  • 属性android:bottom , android:left , 和 android:right不适用于 <item> <layer-list> 中的元素.如果这些是填充,那么你需要使用 <padding> <shape> 中的元素元素。如果这些是插图,则需要使用 <inset> <item> 内的元素元素。
  • 属性android:state_window_focusedandroid:state_focused不适用于 <item> <layer-list> 中的元素.它们用于 item selector 中的元素元素。

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

关于android - 为 EditText 背景加载可绘制 xml 时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25738160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com