2

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

enter image description here

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,

enter image description here

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

Community
  • 1
  • 1
Aniket
  • 2,204
  • 5
  • 34
  • 51
  • This Exception happens exactly because canvas is null. I don't see a reason why it should be null in the onDraw method unless the method is called with a null argument, so you should next try to figure out why it's called with a null canvas. Could you post CandidateView.setSuggestions next? – kviiri Oct 28 '13 at 18:00

3 Answers3

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.

ramblinjan
  • 6,578
  • 3
  • 30
  • 38
  • Thanks for reply...i know that i am passing null value that's why it giving me nullPointerException. So i posted my answer below. – Aniket Oct 29 '13 at 06:12
1

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

Aniket
  • 2,204
  • 5
  • 34
  • 51
0

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

Community
  • 1
  • 1
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300