gpt4 book ai didi

java - 当我触摸屏幕时 Android 应用程序崩溃 - nullPointerException

转载 作者:行者123 更新时间:2023-12-01 12:12:58 26 4
gpt4 key购买 nike

所以我使用 eclipse 构建我的第一个应用程序,这只是一个简单的 Brickbraker 应用程序。该应用程序看起来不错,但当我触摸屏幕时,应用程序崩溃了......请帮我!!主要 Activity :

public class MainActivity extends Activity {
private Paddle paddle;

@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Point p=new Point();
this.getWindowManager().getDefaultDisplay().getSize(p);
int screenW=p.x;
int screenH=p.y;
Ball ball=new Ball(screenW/2,screenH/2,10,screenW,screenH);
Paddle paddle=new Paddle(screenW/2,(float)(0.85*screenH),screenW, 20,90);
ballView bv=new ballView(this,ball);
PaddleView pv=new PaddleView(this,paddle);
gameView gv=new gameView(this);
gv.setbv(bv,pv);
setContentView(gv);
ballMover ballmove=new ballMover(gv,ball);
PaddleMover paddlemove=new PaddleMover(gv,paddle);
ballmove.start();
paddlemove.start();
}

public boolean onTouchEvent(MotionEvent event)
{
this.paddle.setdest(event.getX());
return true;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

桨类: 公开课桨{

private float x; 
private float y;
private int screenW;
private float destination;
private float weidth;
private float height;
public Paddle(float x,float y,int screenW,float height,float weidth)
{

this.x=x;
this.y=y;
this.weidth=weidth;
this.height=height;
this.screenW=screenW;
this.destination=screenW/2;
}
public float left()
{
return this.x-(this.weidth/2);
}
public float right()
{
return this.x+(this.weidth/2);

}
public float top()
{
return this.y-(this.height/2);

}
public float bottom()
{
return this.y+(this.height/2);
}
public void setdest(float destination)
{
this.destination=destination;
}
public void movePaddle()
{
if (left()>=0&&left()-1>destination)
x--;
else
if (right()<=this.screenW&&right()+1<destination)
x++;
}

}

日志猫:

11-27 13:23:23.910: I/Choreographer(933): Skipped 33 frames!  The application may be doing too much work on its main thread.
11-27 13:23:23.960: D/gralloc_goldfish(933): Emulator without GPU emulation detected.
11-27 13:23:24.450: I/Choreographer(933): Skipped 67 frames! The application may be doing too much work on its main thread.
11-27 13:23:24.710: I/Choreographer(933): Skipped 45 frames! The application may be doing too much work on its main thread.
11-27 13:23:24.820: I/Choreographer(933): Skipped 36 frames! The application may be doing too much work on its main thread.
11-27 13:23:25.070: I/Choreographer(933): Skipped 50 frames! The application may be doing too much work on its main thread.
11-27 13:23:25.680: E/InputEventReceiver(933): Exception dispatching input event.
11-27 13:23:25.680: E/MessageQueue-JNI(933): Exception in MessageQueue callback: handleReceiveCallback
11-27 13:23:25.790: E/MessageQueue-JNI(933): java.lang.NullPointerException
11-27 13:23:25.790: E/MessageQueue-JNI(933): at com.example.batandball.MainActivity.onTouchEvent(MainActivity.java:36)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.app.Activity.dispatchTouchEvent(Activity.java:2461)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.View.dispatchPointerEvent(View.java:7886)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.os.MessageQueue.nativePollOnce(Native Method)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.os.MessageQueue.next(MessageQueue.java:138)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.os.Looper.loop(Looper.java:123)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at java.lang.reflect.Method.invoke(Method.java:515)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-27 13:23:25.790: E/MessageQueue-JNI(933): at dalvik.system.NativeStart.main(Native Method)
11-27 13:23:25.800: D/AndroidRuntime(933): Shutting down VM
11-27 13:23:25.800: W/dalvikvm(933): threadid=1: thread exiting with uncaught exception (group=0xb2abbba8)
11-27 13:23:25.930: E/AndroidRuntime(933): FATAL EXCEPTION: main
11-27 13:23:25.930: E/AndroidRuntime(933): Process: com.example.batandball, PID: 933
11-27 13:23:25.930: E/AndroidRuntime(933): java.lang.NullPointerException
11-27 13:23:25.930: E/AndroidRuntime(933): at com.example.batandball.MainActivity.onTouchEvent(MainActivity.java:36)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.app.Activity.dispatchTouchEvent(Activity.java:2461)
11-27 13:23:25.930: E/AndroidRuntime(933): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.View.dispatchPointerEvent(View.java:7886)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.os.MessageQueue.nativePollOnce(Native Method)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.os.MessageQueue.next(MessageQueue.java:138)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.os.Looper.loop(Looper.java:123)
11-27 13:23:25.930: E/AndroidRuntime(933): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-27 13:23:25.930: E/AndroidRuntime(933): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 13:23:25.930: E/AndroidRuntime(933): at java.lang.reflect.Method.invoke(Method.java:515)
11-27 13:23:25.930: E/AndroidRuntime(933): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-27 13:23:25.930: E/AndroidRuntime(933): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-27 13:23:25.930: E/AndroidRuntime(933): at dalvik.system.NativeStart.main(Native Method)
11-27 13:23:31.240: I/Process(933): Sending signal. PID: 933 SIG: 9

问题是什么?

最佳答案

Paddle paddle=new Paddle(screenW/2,(float)(0.85*screenH),screenW, 20,90);

您是shadowing你的桨类领域。因此,当您调用 onTouch 时,paddle(类字段)仍然未初始化(因此使用默认值 null)。

删除类声明:

paddle=new Paddle(screenW/2,(float)(0.85*screenH),screenW, 20,90);

关于java - 当我触摸屏幕时 Android 应用程序崩溃 - nullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27177762/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com