Setting keyPreviewLayout for android virtual keyboard is causing a crash

天涯浪子 提交于 2019-12-23 00:53:16

问题


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

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.

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> 

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>

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.

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


回答1:


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" />

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



来源:https://stackoverflow.com/questions/16031438/setting-keypreviewlayout-for-android-virtual-keyboard-is-causing-a-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!