這裏是我的代碼:Android SDK中自定義鍵盤,設置背景和文本顏色:一個鍵
公共類CustomKeyboardView擴展KeyboardView {
public CustomKeyboardView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paintBackground = new Paint();
paintBackground.setTextAlign(Paint.Align.CENTER);
paintBackground.setTextSize(48);
paintBackground.setColor(Color.YELLOW);
List<Key> keys = getKeyboard().getKeys();
for(Key key: keys) {
if(key.label != null && key.codes[0] == 110)
canvas.drawRect(key.x, key.y, key.x+key.width, key.y+key.height, paintBackground);
paintBackground.setColor(Color.WHITE);
canvas.drawText(key.label.toString(), key.x + (key.width/2),
key.y + (key.height/2), paintBackground);
}
}
}
奇怪的是,如果我註釋掉帆布drawRect中或帆布的drawText,另一個工作正常,但如果我離開他們都在,我得到這個錯誤:
FATAL EXCEPTION: main Process: com.alpha.alphaproto, PID: 6540
java.lang.NullPointerException
at com.alpha.alphaproto.CustomKeyboardView.onDraw(CustomKeyboardView.java:48)
at android.view.View.draw(View.java:14465)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13357)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14468)
at android.widget.FrameLayout.draw(FrameLayout.java:472)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13357)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14468)
at android.widget.FrameLayout.draw(FrameLayout.java:472)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2326)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1570)
at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1449)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2377)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2249)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1879)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method
我大惑不解。任何幫助將非常感激。