为Android虚拟键盘设置keyPreviewLayout会导致崩溃

问题描述:

我在android中有一个自定义虚拟键盘​​,单击按钮时会出现.

I have a custom virtual keyboard in android which appears when a button is clicked.

如果我用语句android:keyPreviewLayout="@layout/mykeypreviewlayout"设置了keyPreviewLayout 并在运行时在res/layout/mykeypreviewlayout.xml中包含一些布局语句,当触摸自定义软键盘上的键时,应用程序将崩溃.

If i set a keyPreviewLayout with the statement android:keyPreviewLayout="@layout/mykeypreviewlayout" and include some layout statements in res/layout/mykeypreviewlayout.xmlat runtime, when a key on the custom soft keyboard is touched, the app crashes.

以下是代码段:以下是主要xml文件中的代码.

Here are the code snippets: The following is from the main xml file.

  <LinearLayout android:layout_height="wrap_content"
  android:layout_gravity="bottom"  
  android:layout_width="wrap_content">  
  <android.inputmethodservice.KeyboardView  
    android:id="@+id/keyboardView" android:visibility="gone"
    android:focusable="true" android:focusableInTouchMode="true"
    android:layout_height="wrap_content" android:layout_width="wrap_content"  
    android:keyPreviewLayout="@layout/mykeypreviewlayout"
    android:layout_weight="0" />
  </LinearLayout> 

这是'res/layout/mykeypreviewlayout.xml中的xml代码

Here is the xml code from 'res/layout/mykeypreviewlayout.xml

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:textColor="@color/black"> </TextView>

如果从上面的KeyboardView布局中删除了设置keyPreviewLayout attr的行,则应用正常运行.但是,当在按下的字符上方弹出按键反馈预览窗口时,该窗口为空白,并且不显示任何字符-只会出现一个带有白色背景的小矩形字符大小的弹出窗口.
如果我添加设置keyPreviewLayout的行,则在软键盘上触摸某个键时,应用程序将崩溃.

If the line setting the keyPreviewLayout attr is removed from the KeyboardView layout above, then app runs normally. However, when the key press feedback preview window popsup above the character pressed, the window is blank, and no characters are displayed - just a small rectangular char-sized popup window with a white background appears.
If i add the line setting the keyPreviewLayout, then the app crashes when a key is touched on the softkeyboard.

这是logcat中堆栈跟踪的转储(相关的前几行),显示崩溃发生在KeyboardView.java内部

Here is the dump of the stacktrace from logcat (first few lines that are relevant) showing that the crash happens inside KeyboardView.java

04-17 07:41:47.346: E/AndroidRuntime(11901): FATAL EXCEPTION: main
04-17 07:41:47.346: E/AndroidRuntime(11901): java.lang.NullPointerException
04-17 07:41:47.346: E/AndroidRuntime(11901):    at                     android.inputmethodservice.KeyboardView.showKey(KeyboardView.java:918)
04-17 07:41:47.346: E/AndroidRuntime(11901):    at     android.inputmethodservice.KeyboardView.access$100(KeyboardView.java:65)
04-17 07:41:47.346: E/AndroidRuntime(11901):    at      android.inputmethodservice.KeyboardView$1.handleMessage(KeyboardView.java:251)
04-17 07:41:47.346: E/AndroidRuntime(11901):    at     android.os.Handler.dispatchMessage(Handler.java:99)
04-17 07:41:47.346: E/AndroidRuntime(11901):    at     android.os.Looper.loop(Looper.java:123)
04-17 07:41:47.346: E/AndroidRuntime(11901):    at android.app.ActivityThread.main(ActivityThread.java:3683)
04-17 07:41:47.346: E/AndroidRuntime(11901):    at java.lang.reflect.Method.invokeNative(Native Method)
04-17 07:41:47.346: E/AndroidRuntime(11901):    at java.lang.reflect.Method.invoke(Method.java:507)
04-17 07:41:47.346: E/AndroidRuntime(11901):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-17 07:41:47.346: E/AndroidRuntime(11901):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

以前有人遇到过此问题吗?任何帮助,将不胜感激.谢谢

Has anyone encountered this problem before ? Any help would be appreciated. thanks

您可以尝试将预览版式更改为以下内容吗:

Can you try changing the preview layout to something like this:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:background="@android:color/white"
          android:textColor="@android:color/black"
          android:gravity="center"
          android:textSize="32sp" />

您不需要使用整个内容,只需将textColor字段更改为我上面的内容即可.它应该为您工作.

You don't need to use the whole thing, just change the textColor field to what I have above. It should work for you.