Каков правильный способ использования LinearLayout для KeyboardView?

Я попытался создать этот класс сервисов ввода:

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:

java,android,

1

Ответов: 1


1 принят

Я предполагаю, что ваш родительский взгляд 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, Android,
Похожие вопросы
Яндекс.Метрика