ClassCastException: android.inputmethodservice.KeyboardView 无法转换为 com.support.mukhtar.simplekeyboard.CustomKeyboardView android
ClassCastException: android.inputmethodservice.KeyboardView cannot be cast to com.support.mukhtar.simplekeyboard.CustomKeyboardView android
我有 SimpleIME.kt class 来实现简单的 InputMethodEditor 并在 android 上做我的自定义键盘:
class SimpleIME : InputMethodService(), OnKeyboardActionListener {
private var kv: CustomKeyboardView? = null
private var keyboard: Keyboard? = null
companion object{
const val TAG :String = "myLogs"
}
private var caps = false
override fun onCreateInputView(): View? {
kv = layoutInflater.inflate(R.layout.keyboard, null) as CustomKeyboardView
keyboard = Keyboard(this, R.xml.qwerty)
kv!!.keyboard = keyboard
kv!!.setOnKeyboardActionListener(this)
Log.d(TAG,"onCreateInputView")
return kv
}
...
}
和我的 CustomKeuboardView class 如下:
class CustomKeyboardView : KeyboardView {
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle)
override fun onKeyMultiple(keyCode: Int, repeatCount: Int, event: KeyEvent?): Boolean {
return super.onKeyMultiple(keyCode, repeatCount, event)
}
override fun onTouchEvent(me: MotionEvent?): Boolean {
Log.d(TAG,"onTouchEvent "+me.toString())
return super.onTouchEvent(me)
}
}
它抛出 classCastException 如下:
java.lang.ClassCastException: android.inputmethodservice.KeyboardView cannot be cast to com.support.mukhtar.simplekeyboard.CustomKeyboardView
at com.support.mukhtar.simplekeyboard.SimpleIME.onCreateInputView(SimpleIME.kt:30)
at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1248)
at android.inputmethodservice.InputMethodService.showWindowInner(InputMethodService.java:1669)
at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:1636)
at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:497)
at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:202)
at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:40)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5426)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
如果我实现 SimpleIME.kt class 如下:
class SimpleIME : InputMethodService(), OnKeyboardActionListener {
private var kv: KeyboardView? = null
private var keyboard: Keyboard? = null
companion object{
const val TAG :String = "myLogs"
}
private var caps = false
override fun onCreateInputView(): View? {
kv = layoutInflater.inflate(R.layout.keyboard, null) as KeyboardView
keyboard = Keyboard(this, R.xml.qwerty)
kv!!.keyboard = keyboard
kv!!.setOnKeyboardActionListener(this)
Log.d(TAG,"onCreateInputView")
return kv
}
...
}
它工作正常,但我需要在 KeyboardView 中实现 onTouchEvent 以处理多次点击,所以我认为有必要实现 CustomKeyboardView Class;
java中有一个CustomKeyboardView的例子:https://github.com/blackcj/AndroidCustomKeyboard.git
请帮助
我的问题与此不同:java.lang.ClassCastException: android.inputmethodservice.KeyboardView cannot be cast to android.view.ViewGroup
因为 layout.xml 有问题,但我没有遇到任何问题,因为如果我像之前显示的那样使用 KeyboardView,它就可以工作
我的layout.keyboard.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<android.inputmethodservice.KeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#3Aad1a"
android:keyPreviewLayout ="@layout/preview"
/>
只需将 android.inputmethodservice.KeyboardView
替换为 CustomKeyboardView
R.layout.keyboard xml 文件中的 CustomKeyboardView
。
我有 SimpleIME.kt class 来实现简单的 InputMethodEditor 并在 android 上做我的自定义键盘:
class SimpleIME : InputMethodService(), OnKeyboardActionListener {
private var kv: CustomKeyboardView? = null
private var keyboard: Keyboard? = null
companion object{
const val TAG :String = "myLogs"
}
private var caps = false
override fun onCreateInputView(): View? {
kv = layoutInflater.inflate(R.layout.keyboard, null) as CustomKeyboardView
keyboard = Keyboard(this, R.xml.qwerty)
kv!!.keyboard = keyboard
kv!!.setOnKeyboardActionListener(this)
Log.d(TAG,"onCreateInputView")
return kv
}
...
}
和我的 CustomKeuboardView class 如下:
class CustomKeyboardView : KeyboardView {
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle)
override fun onKeyMultiple(keyCode: Int, repeatCount: Int, event: KeyEvent?): Boolean {
return super.onKeyMultiple(keyCode, repeatCount, event)
}
override fun onTouchEvent(me: MotionEvent?): Boolean {
Log.d(TAG,"onTouchEvent "+me.toString())
return super.onTouchEvent(me)
}
}
它抛出 classCastException 如下:
java.lang.ClassCastException: android.inputmethodservice.KeyboardView cannot be cast to com.support.mukhtar.simplekeyboard.CustomKeyboardView
at com.support.mukhtar.simplekeyboard.SimpleIME.onCreateInputView(SimpleIME.kt:30)
at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1248)
at android.inputmethodservice.InputMethodService.showWindowInner(InputMethodService.java:1669)
at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:1636)
at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:497)
at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:202)
at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:40)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5426)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
如果我实现 SimpleIME.kt class 如下:
class SimpleIME : InputMethodService(), OnKeyboardActionListener {
private var kv: KeyboardView? = null
private var keyboard: Keyboard? = null
companion object{
const val TAG :String = "myLogs"
}
private var caps = false
override fun onCreateInputView(): View? {
kv = layoutInflater.inflate(R.layout.keyboard, null) as KeyboardView
keyboard = Keyboard(this, R.xml.qwerty)
kv!!.keyboard = keyboard
kv!!.setOnKeyboardActionListener(this)
Log.d(TAG,"onCreateInputView")
return kv
}
...
}
它工作正常,但我需要在 KeyboardView 中实现 onTouchEvent 以处理多次点击,所以我认为有必要实现 CustomKeyboardView Class; java中有一个CustomKeyboardView的例子:https://github.com/blackcj/AndroidCustomKeyboard.git 请帮助
我的问题与此不同:java.lang.ClassCastException: android.inputmethodservice.KeyboardView cannot be cast to android.view.ViewGroup
因为 layout.xml 有问题,但我没有遇到任何问题,因为如果我像之前显示的那样使用 KeyboardView,它就可以工作
我的layout.keyboard.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<android.inputmethodservice.KeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#3Aad1a"
android:keyPreviewLayout ="@layout/preview"
/>
只需将 android.inputmethodservice.KeyboardView
替换为 CustomKeyboardView
R.layout.keyboard xml 文件中的 CustomKeyboardView
。