我正在尝试为我的KeyboardView设置自定义背景。设置XML属性keyBackground似乎是解决这个问题的方法,但是当我尝试给drawable充气时,我在logcat中不断得到NullPointerException斜杠ResourceNotFoundException。我不确定是什么问题 - 所有文件似乎都在正确的位置。我把它们放在多个可绘制的文件夹中,清理了我的项目等等。
main.xml文件:
<android.inputmethodservice.KeyboardView
android:id="@+id/keyboardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:visibility="gone"
android:keyBackground="@drawable/keybg" />
keybg.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_pressed="true"
android:drawable="@drawable/keybg_pressed" />
<item
android:drawable="@drawable/keybg_notpressed" />
</selector>
keybg_pressed和keybg_notpressed都是png文件
(相关)logcat:
E/AndroidRuntime(17772): FATAL EXCEPTION: main
E/AndroidRuntime(17772): java.lang.RuntimeException: Unable to start activity
ComponentInfo{MainActivity}: android.view.InflateException:
Binary XML file line #620: Error inflating class
android.inputmethodservice.KeyboardView
E/AndroidRuntime(17772): Caused by: android.content.res.Resources$NotFoundException: File
res/drawable/keybg.xml from drawable resource ID #0x7f0201a3
E/AndroidRuntime(17772): at
android.content.res.Resources.loadDrawable(Resources.java:1947)
E/AndroidRuntime(17772): at
android.content.res.TypedArray.getDrawable(TypedArray.java:601)
E/AndroidRuntime(17772): at android.inputmethodservice.KeyboardView.<init>
(KeyboardView.java:303)
E/AndroidRuntime(17772): at android.inputmethodservice.KeyboardView.<init>
(KeyboardView.java:279)
E/AndroidRuntime(17772): ... 28 more
E/AndroidRuntime(17772): Caused by: java.lang.NullPointerException
E/AndroidRuntime(17772): at
android.graphics.drawable.DrawableContainer$DrawableContainerState.
addChild(DrawableContainer.java:524)
E/AndroidRuntime(17772): at
android.graphics.drawable.StateListDrawable$StateListState.addStateSet(StateListDrawable.java:278)
E/AndroidRuntime(17772): at
android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:186)
E/AndroidRuntime(17772): at
android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881)
E/AndroidRuntime(17772): at
android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
E/AndroidRuntime(17772): at
android.content.res.Resources.loadDrawable(Resources.java:1944)
E/AndroidRuntime(17772): ... 31 more
答案 0 :(得分:1)
你说&#34; keybg_pressed和keybg_notpressed都是png文件&#34;。
它们应该是XML文件! (我知道这很奇怪。)
我将其用于自定义键盘视图:
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/keyboardView"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:keyBackground="@drawable/samplekeybackground"
android:keyTextColor="@android:color/white"
android:keyPreviewLayout="@layout/input_key_preview"
android:background="@drawable/keyboardbackground"
答案 1 :(得分:0)
在res中创建一个文件夹并创建一个xml文件,如:
<!-- Functional keys. -->
<item android:state_single="true" android:state_pressed="true"
android:drawable="@drawable/btn_keyboard_key_dark_pressed_holo" />
<item android:state_single="true"
android:drawable="@drawable/btn_keyboard_key_dark_normal_holo" />
<!-- Toggle keys. Use checkable/checked state. -->
<!-- The lock icon is a copy of the 'on' icon, hue shifted by +164 using Gimp's Colors/Hue&Saturation -->
<item android:state_checkable="true" android:state_checked="true" android:state_pressed="true"
android:drawable="@drawable/btn_keyboard_key_dark_pressed_on_holo" />
<item android:state_checkable="true" android:state_pressed="true"
android:drawable="@drawable/btn_keyboard_key_dark_pressed_off_holo" />
<item android:state_checkable="true" android:state_checked="true" android:state_active="true"
android:drawable="@drawable/btn_keyboard_key_dark_normal_lock_holo" />
<item android:state_checkable="true" android:state_checked="true"
android:drawable="@drawable/btn_keyboard_key_dark_normal_on_holo" />
<item android:state_checkable="true"
android:drawable="@drawable/btn_keyboard_key_dark_normal_off_holo" />
<!-- Normal keys. -->
<item android:state_pressed="true"
android:drawable="@drawable/btn_keyboard_key_light_pressed_holo" />
<item android:drawable="@drawable/btn_keyboard_key_light_normal_holo" />
以上图片存在于drawable-hdi png
中