我正在为我的键盘视图设置自定义背景。设置xml属性keyBackground似乎是解决这个问题的方法,但是当logcat试图对drawable进行膨胀时,我在logcat中不断收到nullpointerexception slash 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

最佳答案

你说“keybg_pressed和keybg_notpressed都是png文件”。
它们应该是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"

10-07 19:48
查看更多