gpt4 book ai didi

java - 将 LinearLayout 转换到 KeyboardView 的正确方法是什么?

转载 作者:行者123 更新时间:2023-11-30 00:17:23 27 4
gpt4 key购买 nike

我试图创建这个输入服务类:

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

最佳答案

我猜,R.layout.keyboard_view 的父 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>

关于java - 将 LinearLayout 转换到 KeyboardView 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47003027/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com