我在 android 中有一个自定义虚拟键盘,单击按钮时会出现该键盘。
如果我keyPreviewLayout在语句中设置 aandroid:keyPreviewLayout="@layout/mykeypreviewlayout"
并res/layout/mykeypreviewlayout.xml在运行时包含一些布局语句,当触摸自定义软键盘上的某个键时,应用程序会崩溃。
以下是代码片段: 以下来自主 xml 文件。
<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>
Run Code Online (Sandbox Code Playgroud)
这是来自'res/layout/mykeypreviewlayout.xml的xml代码
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:textColor="@color/black"> </TextView>
Run Code Online (Sandbox Code Playgroud)
如果从上面的 KeyboardView 布局中删除设置 keyPreviewLayout attr 的行,则应用程序正常运行。但是,当按键反馈预览窗口在按下的字符上方弹出时,该窗口是空白的,并且没有显示任何字符——只出现一个带有白色背景的小矩形字符大小的弹出窗口。
如果我添加设置 keyPreviewLayout 的行,那么当在软键盘上触摸某个键时应用程序会崩溃。
这是 logcat 的堆栈跟踪的转储(相关的前几行)显示崩溃发生在 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 …Run Code Online (Sandbox Code Playgroud)