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.