我尝试创建此输入服务类:
package sandhiflowers.game.com;
import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputConnection;
/**
* Created by vlado on 16.10.17.
*/
public class SandhiFlowersInputMethodService extends InputMethodService implements KeyboardView.OnKeyboardActionListener {
@Override
public View onCreateInputView() {
// get the KeyboardView and add our Keyboard layout to it
KeyboardView keyboardView = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard_view, null);
Keyboard keyboard = new Keyboard(this, R.xml.number_pad);
keyboardView.setKeyboard(keyboard);
keyboardView.setOnKeyboardActionListener(this);
return keyboardView;
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
InputConnection ic = getCurrentInputConnection();
if (ic == null) {
return;
}
switch (primaryCode) {
case Keyboard.KEYCODE_DELETE:
CharSequence selectedText = ic.getSelectedText(0);
if (TextUtils.isEmpty(selectedText)) {
// no selection, so delete previous character
ic.deleteSurroundingText(1, 0);
} else {
// delete the selection
ic.commitText("", 1);
}
ic.deleteSurroundingText(1, 0);
break;
default:
char code = (char) primaryCode;
ic.commitText(String.valueOf(code), 1);
}
}
@Override
public void onPress(int primaryCode) { }
@Override
public void onRelease(int primaryCode) { }
@Override
public void onText(CharSequence text) { }
@Override
public void swipeLeft() { }
@Override
public void swipeRight() { }
@Override
public void swipeDown() { }
@Override
public void swipeUp() { }
}
我得到了这个类强制转换异常:
10-29 17:41:49.133 7804-7804/sandhiflowers.game.com E/AndroidRuntime: FATAL EXCEPTION: main
Process: sandhiflowers.game.com, PID: 7804
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.inputmethodservice.KeyboardView
at sandhiflowers.game.com.SandhiFlowersInputMethodService.onCreateInputView(SandhiFlowersInputMethodService.java:19)
at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1135)
at android.inputmethodservice.InputMethodService.showWindowInner(InputMethodService.java:1498)
at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:1473)
at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:452)
at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:203)
at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5624)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
这是导致错误的行:
KeyboardView keyboardView = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard_view, null);
和此:
10-29 18:46:50.997 18888-18888/? I/art: Late-enabling -Xcheck:jni
10-29 18:46:51.033 18888-18888/? W/Environment: userEnvironment:hasSdcard:true,isswitch:true
10-29 18:46:51.100 18888-18888/sandhiflowers.game.com D/ActivityThread: hoder:android.app.IActivityManager$ContentProviderHolder@dd6e5e4,provider,holder.Provider:android.content.ContentProviderProxy@10e38a4d
10-29 18:46:51.115 18888-18888/sandhiflowers.game.com D/Proxy: setHttpRequestCheckHandler
10-29 18:46:51.650 18888-18888/sandhiflowers.game.com D/ActivityThread: installProvider: context.getPackageName()=sandhiflowers.game.com
10-29 18:46:51.660 18888-18888/sandhiflowers.game.com I/InstantRun: starting instant run server: is main process
10-29 18:46:51.670 18888-18888/sandhiflowers.game.com D/ActivityThread: BIND_APPLICATION handled : 0 / AppBindData{appInfo=ApplicationInfo{390e7c50 sandhiflowers.game.com}}
10-29 18:46:51.750 18888-18888/sandhiflowers.game.com D/ActivityThread: SVC-Creating service: CreateServiceData{token=android.os.BinderProxy@2aeb7e49 className=sandhiflowers.game.com.SandhiFlowersInputMethodService packageName=sandhiflowers.game.com intent=null}
10-29 18:46:51.754 18888-18888/sandhiflowers.game.com D/FeatureProxyBase: FeatureProxyBase class constructor
10-29 18:46:51.755 18888-18888/sandhiflowers.game.com D/MultiWindow: MultiWindowProxy constructor.
10-29 18:46:51.755 18888-18888/sandhiflowers.game.com D/FeatureProxyBase: getService(), serviceName = multiwindow_service_v1
10-29 18:46:51.756 18888-18888/sandhiflowers.game.com D/FeatureProxyBase: FeatureProxyBase class constructor
10-29 18:46:51.756 18888-18888/sandhiflowers.game.com D/MultiWindow: MultiWindowProxy constructor.
10-29 18:46:51.756 18888-18888/sandhiflowers.game.com D/FeatureProxyBase: getService(), serviceName = multiwindow_service_v1
10-29 18:46:51.773 18888-18888/sandhiflowers.game.com D/AccessibilityManager: setStateLocked: wasEnabled = false, mIsEnabled = false, wasTouchExplorationEnabled = false, mIsTouchExplorationEnabled = false, wasHighTextContrastEnabled = false, mIsHighTextContrastEnabled = false
java.lang.Throwable: setStateLocked
at android.view.accessibility.AccessibilityManager.setStateLocked(AccessibilityManager.java:553)
at android.view.accessibility.AccessibilityManager.tryConnectToServiceLocked(AccessibilityManager.java:636)
at android.view.accessibility.AccessibilityManager.<init>(AccessibilityManager.java:226)
at android.view.accessibility.AccessibilityManager.getInstance(AccessibilityManager.java:206)
at android.view.View.setFlags(View.java:9843)
at android.view.ViewGroup.initViewGroup(ViewGroup.java:536)
at android.view.ViewGroup.<init>(ViewGroup.java:525)
at android.widget.LinearLayout.<init>(LinearLayout.java:215)
at android.widget.LinearLayout.<init>(LinearLayout.java:211)
at android.widget.LinearLayout.<init>(LinearLayout.java:206)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.inputmethodservice.InputMethodService.initViews(InputMethodService.java:731)
at android.inputmethodservice.InputMethodService.onCreate(InputMethodService.java:703)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2982)
at android.app.ActivityThread.access$1800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1553)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5624)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
10-29 18:46:51.801 18888-18888/sandhiflowers.game.com V/SettingsInterface: invalidate [global]: current 2 != cached 0
10-29 18:46:51.802 18888-18888/sandhiflowers.game.com D/ActivityThread: SVC-CREATE_SERVICE handled : 0 / CreateServiceData{token=android.os.BinderProxy@2aeb7e49 className=sandhiflowers.game.com.SandhiFlowersInputMethodService packageName=sandhiflowers.game.com intent=null}
10-29 18:46:51.807 18888-18888/sandhiflowers.game.com D/ActivityThread: SVC-BIND_SERVICE handled : 0 / BindServiceData{token=android.os.BinderProxy@2aeb7e49 intent=Intent { act=android.view.InputMethod cmp=sandhiflowers.game.com/.SandhiFlowersInputMethodService }}
和此:
10-29 18:57:54.264 26752-26752/sandhiflowers.game.com E/AndroidRuntime: FATAL EXCEPTION: main
Process: sandhiflowers.game.com, PID: 26752
android.view.InflateException: Binary XML file line #2: Error inflating class com.game.sandhiflowers.SandhiFlowersSoftKeyboard
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at sandhiflowers.game.com.SandhiFlowersInputMethodService.onCreateInputView(SandhiFlowersInputMethodService.java:19)
at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1135)
at android.inputmethodservice.InputMethodService.showWindowInner(InputMethodService.java:1498)
at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:1473)
at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:452)
at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:203)
at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5624)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.game.sandhiflowers.SandhiFlowersSoftKeyboard" on path: DexPathList[[zip file "/data/app/sandhiflowers.game.com-1/base.apk", zip file "/data/app/sandhiflowers.game.com-1/split_lib_dependencies_apk.apk", zip file "/data/app/sandhiflowers.game.com-1/split_lib_slice_0_apk.apk", zip file "/data/app/sandhiflowers.game.com-1/split_lib_slice_1_apk.apk", zip file "/data/app/sandhiflowers.game.com-1/split_lib_slice_2_apk.apk", zip file "/data/app/sandhiflowers.game.com-1/split_lib_slice_3_apk.apk", zip file "/data/app/sandhiflowers.game.com-1/split_lib_slice_4_apk.apk", zip file "/data/app/sandhiflowers.game.com-1/split_lib_slice_5_apk.apk", zip file "/data/app/sandhiflowers.game.com-1/split_lib_slice_6_apk.apk", zip file "/data/app/sandhiflowers.game.com-1/split_lib_slice_7_apk.apk", zip file "/data/app/sandhiflowers.game.com-1/split_lib_slice_8_apk.apk", zip file "/data/app/sandhiflowers.game.com-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.view.LayoutInflater.createView(LayoutInflater.java:571)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at sandhiflowers.game.com.SandhiFlowersInputMethodService.onCreateInputView(SandhiFlowersInputMethodService.java:19)
at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1135)
at android.inputmethodservice.InputMethodService.showWindowInner(InputMethodService.java:1498)
at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:1473)
at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:452)
at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:203)
at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5624)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Suppressed: java.lang.ClassNotFoundException: com.game.sandhiflowers.SandhiFlowersSoftKeyboard
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 20 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
我很惊讶我在编码时没有在Android Studio中收到任何编译错误。
将LinearLayout强制转换为KeyboardView的正确方法是什么?或者我应该使用其他课程?
注意:我使用的是Android Studio 2.3和Android 5.1
答案 0 :(得分:1)
我猜测,R.layout.keyboard_view
的父视图应该是KeyboardView
,而不是LinearLayout
。变化:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
为:
<your.packages.KeyboardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</your.packages.KeyboardView>