这是我之前提出的问题的后续跟进。基本上我有一个游戏,其中主要动作发生在画布上,该画布位于从View扩展的自定义类中。
我将分数保存在字符串中,我试图将其绘制到屏幕的左上角。
在我的onDraw()方法的底部,我有以下代码行:
canvas.drawText(score, 5, 5, null);
当我尝试运行游戏时,log cat会给我以下错误:
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): FATAL EXCEPTION: main
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): java.lang.NullPointerException
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.graphics.Canvas.drawText(Native Method)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at com.mattdrewery.supercatch.GameView.onDraw(GameView.java:143)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.View.draw(View.java:6880)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.View.draw(View.java:6883)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.widget.FrameLayout.draw(FrameLayout.java:357)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.View.draw(View.java:6883)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.widget.FrameLayout.draw(FrameLayout.java:357)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.ViewRoot.draw(ViewRoot.java:1522)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.os.Looper.loop(Looper.java:123)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at java.lang.reflect.Method.invoke(Method.java:507)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:9)
您的Paint参数不能为空。
在某处初始化它(不在onDraw中);
Paint mPaint = new Paint();
canvas.drawText(score, 5, 5, mPaint);