本文介绍了软键盘在调用canvas.drawtext时获取NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

中所述此参考此参考,我正在使用以下代码,

As given in this reference and this reference, I am using below code,

 protected void onDraw(Canvas canvas) {
        if (canvas != null) {
            super.onDraw(canvas);
        }

        Paint paint = new Paint();
        paint.setTextSize(15);
        paint.setColor(Color.RED);

        int x2 = 0;
        int y2 = 0;
        int width = 0;
        List<Key> keys = SoftKeyboard.currentKeyboard.getKeys();
        for(Key key: keys) {
        if(key.codes[0] == 113)
            x2 = key.x; // value of x2 = 0;
            y2 = key.y; // value of y2 = 0;
            width = key.width; // value of width = 32;
            canvas.drawText("1", x2 + (width/2), y2 + 5, paint); // getting null pointer exception here line 240
        }
    }

>致命异常:位于以下位置的主要java.lang.NullPointerException com.example.android.softkeyboard.CandidateView.onDraw(CandidateView.java:240) 在 com.example.android.softkeyboard.CandidateView.setSuggestions(CandidateView.java:279) 在 com.example.android.softkeyboard.SoftKeyboard.setSuggestions(SoftKeyboard.java:597) 在 com.example.android.softkeyboard.SoftKeyboard.updateCandidates(SoftKeyboard.java:582) 在 com.example.android.softkeyboard.SoftKeyboard.onFinishInput(SoftKeyboard.java:260) 在 android.inputmethodservice.InputMethodService.doFinishInput(InputMethodService.java:1543) 在 android.inputmethodservice.InputMethodService.doStartInput(InputMethodService.java:1552) 在 android.inputmethodservice.InputMethodService $ InputMethodImpl.startInput(InputMethodService.java:390) 在 android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:158) 在 com.android.internal.os.HandlerCaller $ MyHandler.handleMessage(HandlerCaller.java:61) 在android.os.Handler.dispatchMessage(Handler.java:99) android.os.Looper.loop(Looper.java:153)在 android.app.ActivityThread.main(ActivityThread.java:5000)位于 java.lang.reflect.Method.invokeNative(本机方法)位于 java.lang.reflect.Method.invoke(Method.java:511)在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:821) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)处 dalvik.system.NativeStart.main(本机方法)10-19 13:26:04.844: E/Trace(17693):打开跟踪文件时出错:没有这样的文件或目录 (2)

> FATAL EXCEPTION: main java.lang.NullPointerException at com.example.android.softkeyboard.CandidateView.onDraw(CandidateView.java:240) at com.example.android.softkeyboard.CandidateView.setSuggestions(CandidateView.java:279) at com.example.android.softkeyboard.SoftKeyboard.setSuggestions(SoftKeyboard.java:597) at com.example.android.softkeyboard.SoftKeyboard.updateCandidates(SoftKeyboard.java:582) at com.example.android.softkeyboard.SoftKeyboard.onFinishInput(SoftKeyboard.java:260) at android.inputmethodservice.InputMethodService.doFinishInput(InputMethodService.java:1543) at android.inputmethodservice.InputMethodService.doStartInput(InputMethodService.java:1552) at android.inputmethodservice.InputMethodService$InputMethodImpl.startInput(InputMethodService.java:390) at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:158) at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:61) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:153) at android.app.ActivityThread.main(ActivityThread.java:5000) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584) at dalvik.system.NativeStart.main(Native Method) 10-19 13:26:04.844: E/Trace(17693): error opening trace file: No such file or directory (2)

我在调试代码时检查了画布始终为空,所以 我认为由于画布为空,因此出现此异常.

I have checked when i am debugging code the canvas is always null, so I think i am getting this exception due to canvas is null.

因此我由于画布为null或问题所在而收到此异常 其他任何地方.

So i am getting this exception due to canvas is null or problem lies anywhere else.

还有一件事,我也尝试使用popupCharacters和这个 正在工作,但我需要在键盘上输入3个字符,如下所示,

And one thing more i have also tried using popupCharacters and this is working but i need 3 characters on key as shown below,

推荐答案

由于错误,我正在扩展班级的View班.因此,当我使用 LatinKeyboard 解决问题时,我扩展了KeyboardView .

By mistake i am extending View class in my class. So then i extended KeyboardView as i working with LatinKeyboard which solves my issue.

这篇关于软键盘在调用canvas.drawtext时获取NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:16