Soft Keyboard getting NullPointerException when calling canvas.drawtext

 ̄綄美尐妖づ 提交于 2019-12-06 14:17:52

问题


I am working on SoftKeyboard where i need to display text on keys as shown below by red blocks

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
        }
    }

My stack trace is shown below

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

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

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

That's why i am trying to do with paint on canvas but getting exception.


回答1:


Clearly canvas is being passed as null. The problem is where the function you posted is getting called from. This is potentially also causing you problems:

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

You avoid calling the parent if canvas is null, but you don't handle the rest of your code differently? At the very least, I would put the rest of your code inside this conditional block and when canvas is null allow nothing to happen.




回答2:


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




回答3:


NO need to draw it,There is simple way of achieving this .. See detail here.



来源:https://stackoverflow.com/questions/19464703/soft-keyboard-getting-nullpointerexception-when-calling-canvas-drawtext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!