0

我是android游戏开发的新手。在模拟器和我的一台 android 设备上一切正常,但在我的 nexus 4 中崩溃......

我正在尝试使用

@Override
public boolean dispatchTouchEvent(MotionEvent event) {

    if(!jump){
    first=true;
    makeajump();
    }

    return super.dispatchTouchEvent(event);
}

这是在 nexus 上的调试模式测试中发生错误的地方:

jcanvas.drawBitmap(mytree, xtree1,height-t1.getHeight(), null);//inside makeajump() line 404

这一切都有效,直到用户点击屏幕......

jcanvas = canvas;//set in onDraw(),jcanvas is global

发生错误时,这是​​我的 logcat:

06-18 18:40:55.752: E/InputEventReceiver(8523): Exception dispatching input event.
06-18 18:40:55.752: E/MessageQueue-JNI(8523): Exception in MessageQueue callback: handleReceiveCallback
06-18 18:40:55.762: E/MessageQueue-JNI(8523): java.lang.NullPointerException
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.view.GLES20RecordingCanvas.drawBitmap(GLES20RecordingCanvas.java:105)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at lion.run.vjapps.game.animal.GameView.makeajump(GameView.java:404)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at lion.run.vjapps.game.animal.GameView.dispatchTouchEvent(GameView.java:455)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.view.View.dispatchPointerEvent(View.java:7426)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.os.MessageQueue.nativePollOnce(Native Method)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.os.MessageQueue.next(MessageQueue.java:125)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.os.Looper.loop(Looper.java:124)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at android.app.ActivityThread.main(ActivityThread.java:5041)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at java.lang.reflect.Method.invokeNative(Native Method)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at java.lang.reflect.Method.invoke(Method.java:511)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-18 18:40:55.762: E/MessageQueue-JNI(8523):   at dalvik.system.NativeStart.main(Native Method)
06-18 18:40:55.762: D/AndroidRuntime(8523): Shutting down VM
06-18 18:40:55.762: W/dalvikvm(8523): threadid=1: thread exiting with uncaught exception (group=0x41a8d930)
06-18 18:40:55.762: E/AndroidRuntime(8523): FATAL EXCEPTION: main
06-18 18:40:55.762: E/AndroidRuntime(8523): java.lang.NullPointerException
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.view.GLES20RecordingCanvas.drawBitmap(GLES20RecordingCanvas.java:105)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at lion.run.vjapps.game.animal.GameView.makeajump(GameView.java:404)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at lion.run.vjapps.game.animal.GameView.dispatchTouchEvent(GameView.java:455)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.view.View.dispatchPointerEvent(View.java:7426)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.os.MessageQueue.nativePollOnce(Native Method)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.os.MessageQueue.next(MessageQueue.java:125)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.os.Looper.loop(Looper.java:124)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at java.lang.reflect.Method.invokeNative(Native Method)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at java.lang.reflect.Method.invoke(Method.java:511)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-18 18:40:55.762: E/AndroidRuntime(8523):     at dalvik.system.NativeStart.main(Native Method)

任何帮助表示赞赏...非常感谢..

4

1 回答 1

1

你应该把你的绘图代码移到 onDraw()

       canvas.drawBitmap(mytree, xtree1,height-t1.getHeight(), null);

您可以在 onDraw() 之外进行计算并调用invalidate()以刷新绘图

public void invalidate ()

使整个视图无效。如果视图可见,onDraw(android.graphics.Canvas) 将在未来的某个时间点被调用。这必须从 UI 线程调用。要从非 UI 线程调用,请调用 postInvalidate()。

于 2013-06-18T13:57:04.277 回答