3

弃用 KeyboardView 后,我按照谷歌文档进行操作,正如他们所说,我将 KeyboardView 和 Keyboard 类复制到我的项目中。我按照建议设置的所有内容。

问题是当我运行我的应用程序时,它会因抛出错误充气 KeyboardView的错误而崩溃。

这是我的代码。

public class SimpleKB extends InputMethodService implements
    KeyboardView.OnKeyboardActionListener {

private KeyboardView kv;
private Keyboard keyboard;
private Keyboard symbols;
private Keyboard eng_keyboard;

//Core overridden Functions
@Override public View onCreateInputView() {
    kv = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard, null);
    keyboard = new Keyboard(this, R.xml.qwerty);
    symbols = new Keyboard(this, R.xml.symbol);
    eng_keyboard = new Keyboard(this, R.xml.eng_qwerty);
    kv.setKeyboard(keyboard);
    kv.setOnKeyboardActionListener(this);
    return kv;
}

@Override public void onInitializeInterface() {
    if (keyboard != null) {
        // Configuration changes can happen after the keyboard gets recreated,
        // so we need to be able to re-build the keyboards if the available
        // space has changed.
        int displayWidth = getMaxWidth();
        if (displayWidth == mLastDisplayWidth) return;
        mLastDisplayWidth = displayWidth;
    }
    keyboard = new Keyboard(this, R.xml.qwerty);
}

这是用于显示建议单词的 CandidateView 类。

public class CandidateView extends View {

private static final int OUT_OF_BOUNDS = -1;

private SimpleKB mService;
private List<String> mSuggestions;
private int mSelectedIndex;
private int mTouchX = OUT_OF_BOUNDS;
private Drawable mSelectionHighlight;
private boolean mTypedWordValid;

private Rect mBgPadding;

private static final int MAX_SUGGESTIONS = 32;
private static final int SCROLL_PIXELS = 20;

private int[] mWordWidth = new int[MAX_SUGGESTIONS];
private int[] mWordX = new int[MAX_SUGGESTIONS];

private static final int X_GAP = 10;

private static final List<String> EMPTY_LIST = new ArrayList<String>();

private int mColorNormal;
private int mColorRecommended;
private int mColorOther;
private int mVerticalPadding;
private Paint mPaint;
private boolean mScrolled;
private int mTargetScrollX;

private int mTotalWidth;

private GestureDetector mGestureDetector;



/**
 * Construct a CandidateView for showing suggested words for completion.
 * @param context
 */
public CandidateView(Context context) {
    super(context);
    mSelectionHighlight = context.getResources().getDrawable(
            android.R.drawable.list_selector_background);
    mSelectionHighlight.setState(new int[] {
            android.R.attr.state_enabled,
            android.R.attr.state_focused,
            android.R.attr.state_window_focused,
            android.R.attr.state_pressed
    });

    Resources r = context.getResources();

    setBackgroundColor(r.getColor(R.color.candidate_background));

    mColorNormal = r.getColor(R.color.candidate_normal);
    mColorRecommended = r.getColor(R.color.candidate_recommended);
    mColorOther = r.getColor(R.color.candidate_other);
    mVerticalPadding = r.getDimensionPixelSize(R.dimen.candidate_vertical_padding);

    mPaint = new Paint();
    mPaint.setColor(mColorNormal);
    mPaint.setAntiAlias(true);
    mPaint.setTextSize(r.getDimensionPixelSize(R.dimen.candidate_font_height));
    mPaint.setStrokeWidth(0);

    mGestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                float distanceX, float distanceY) {
            mScrolled = true;
            int sx = getScrollX();
            sx += distanceX;
            if (sx < 0) {
                sx = 0;
            }
            if (sx + getWidth() > mTotalWidth) {
                sx -= distanceX;
            }
            mTargetScrollX = sx;
            scrollTo(sx, getScrollY());
            invalidate();
            return true;
        }
    });
    setHorizontalFadingEdgeEnabled(true);
    setWillNotDraw(false);
    setHorizontalScrollBarEnabled(false);
    setVerticalScrollBarEnabled(false);
}

这是我的 xml 布局,带有我的自定义 KeyboardView。

<?xml version="1.0" encoding="UTF-8"?>
<com.android.urdu.KeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/keyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:keyTextSize="15sp"
android:layout_alignParentBottom="true"
android:keyPreviewLayout="@layout/preview" />

这是我得到的错误。

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.urdu, PID: 17584
android.view.InflateException: Binary XML file line #2 in com.android.urdu:layout/keyboard: Binary XML file line #2 in com.android.urdu:layout/keyboard: Error inflating class com.android.urdu.KeyboardView
Caused by: android.view.InflateException: Binary XML file line #2 in com.android.urdu:layout/keyboard: Error inflating class com.android.urdu.KeyboardView
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
    at android.view.LayoutInflater.createView(LayoutInflater.java:858)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1014)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:663)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
    at com.android.urdu.SimpleKB.onCreateInputView(SimpleKB.java:62)
    at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1531)
    at android.inputmethodservice.InputMethodService.prepareWindow(InputMethodService.java:1961)
    at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:1908)
    at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:643)
    at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:220)
    at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:44)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:224)
    at android.app.ActivityThread.main(ActivityThread.java:7561)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.drawable.Drawable.getPadding(android.graphics.Rect)' on a null object reference
    at com.android.urdu.KeyboardView.<init>(KeyboardView.java:278)
    at com.android.urdu.KeyboardView.<init>(KeyboardView.java:200)
    at java.lang.reflect.Constructor.newInstance0(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
    at android.view.LayoutInflater.createView(LayoutInflater.java:858) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1014) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:663) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:538) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:481) 
    at com.android.urdu.SimpleKB.onCreateInputView(SimpleKB.java:62) 
    at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1531) 
    at android.inputmethodservice.InputMethodService.prepareWindow(InputMethodService.java:1961) 
    at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:1908) 
    at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:643) 
    at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:220) 
    at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:44) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:224) 
    at android.app.ActivityThread.main(ActivityThread.java:7561) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995) 

请告诉我我在这方面缺少什么。我需要帮助。

4

1 回答 1

1

堆栈跟踪的相关部分被发现是最后一个原因:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.drawable.Drawable.getPadding(android.graphics.Rect)' on a null object reference
   at com.android.urdu.KeyboardView.<init>(KeyboardView.java:278)
   at com.android.urdu.KeyboardView.<init>(KeyboardView.java:200)

这基本上意味着该keyBackground属性未设置。

mKeyBackground = a.getDrawable(R.styleable.KeyboardView_keyBackground)
mKeyBackground.getPadding(mPadding)

这样做的原因是,您为 定义了新属性KeyboardView,而keyboardViewStyle只配置了框架属性。因此,您必须为所需属性提供自己的默认值。

于 2021-04-19T10:58:12.403 回答