我试图在自定义视图中传递对图像的引用。我认为在布局中定义drawable比在某种类型的列表中定义要简洁得多。但是,我一直得到一个NullPointerException。以下是我目前的代码:
自定义视图:
TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.Button, 0, 0);
try {
float scale = array.getFloat(R.styleable.Button_scale, 1.0f);
keys.put(ButtonState.DOWN, MainActivity.ScaleBitmap(BitmapFactory.decodeResource(getResources(), array.getResourceId(R.styleable.Button_down, 0)), scale));
keys.put(ButtonState.UP, MainActivity.ScaleBitmap(BitmapFactory.decodeResource(getResources(), array.getResourceId(R.styleable.Button_up, 0)), scale));
} finally {
array.recycle();
}
属性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="scale" format="float"/>
<declare-styleable name="Button">
<attr name="scale"/>
<attr name="up" format="reference"/>
<attr name="down" format="reference"/>
</declare-styleable>
</resources>
布局:
<com.lark.app.Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="120dp"
custom:down="@drawable/button_down"
custom:up="@drawable/button_up"
custom:scale="0.5"/>
我已经确认图片确实在可绘制文件夹中。
编辑:Logcat:http://pastebin.com/J3AZ8M5a