处理来自 AOSP 的代码后无法实现 KeyboardView.xml中的错误 [英] Unable to implement KeyboardView after coping the code from AOSP. Error in xml

查看:35
本文介绍了处理来自 AOSP 的代码后无法实现 KeyboardView.xml中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在键盘视图被弃用后,我按照谷歌文档进行操作,正如他们所说,我将键盘视图和键盘类复制到我的项目中.我按照推荐设置的所有内容.

After the KeyboardView is deprecated, I follow the google documentation and as they stated I copied the KeyboardView and Keyboard classes to my project. Everything I setup as its recommended.

问题是当我运行我的应用程序时,它因错误膨胀键盘视图的错误而崩溃.

The problem is when I run my app, it crash by throughing an error of error inflating 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 类.

Here is CandidateView class for showing suggested words.

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 布局,带有我的自定义键盘视图.

And here is my xml layout with have my custom 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) 

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

Please tell me what I'm missing in this. I need help in it.

推荐答案

stacktrace 的相关部分被发现是最后一个原因:

The relevant portion of the stacktrace is found as the last cause:

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 属性.

This basically means that the keyBackground attribute was not set.

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

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

The reason for this is, that you defined new attributes for the KeyboardView, while keyboardViewStyle only configures the framework attributes. So you'll have to provide your own default values for the required attributes.

这篇关于处理来自 AOSP 的代码后无法实现 KeyboardView.xml中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆