我正在尝试做一个基本的游戏。我有一个名为Ball.java,Scratch.java和Panel.java的类
在Panel中,我试图从Ball.java类中获取信息但是我得到了NullPointerException。我的代码中有错误吗?
在Panel.java中的此代码中获得优势
public Rect getBoundsBall(){
return new Rect (top.getX(), top.getY(),top.getX()+ball.getWidth() ,top.getY()+ball.getHeight() );
}
错误记录
10-13 12:57:28.746: E/AndroidRuntime(367): FATAL EXCEPTION: Thread-10
10-13 12:57:28.746: E/AndroidRuntime(367): java.lang.NullPointerException
10-13 12:57:28.746: E/AndroidRuntime(367): at com.emredavarci.denemeler.Panel.getBoundsBall(Panel.java:44)
10-13 12:57:28.746: E/AndroidRuntime(367): at com.emredavarci.denemeler.Panel.update(Panel.java:70)
10-13 12:57:28.746: E/AndroidRuntime(367): at com.emredavarci.denemeler.TutorialThread.run(TutorialThread.java:30)
Panel.java
public class Panel extends SurfaceView implements SurfaceHolder.Callback {
private TutorialThread _thread;
Scratch raket=new Scratch();
Ball top=new Ball();
Bitmap ball;
Bitmap _scratch;
public Panel(Context context) {
super(context);
getHolder().addCallback(this);
_thread = new TutorialThread(getHolder(), this); /
setFocusable(true);
}
@Override
public boolean onTouchEvent(MotionEvent event) { // to
switch (event.getAction()){
case MotionEvent.ACTION_MOVE: //
raket.setX((int)(event.getX()));
break;
}
return true;
}
public Rect getBoundsBall(){
return new Rect (top.getX(), top.getY(),top.getX()+ball.getWidth() ,top.getY()+ball.getHeight() );
}
public Rect getBoundsScratch(){
return new Rect (raket.getX(), raket.getY(),raket.getX()+_scratch.getWidth() ,raket.getY()+_scratch.getHeight() );
}
public void update(){ /
if((top.getX()<480) && (top.getX()>0)){
top.setX(top.getX()+top.getxdirection());
}
if((top.getX()==480) || (top.getX()==0)){
top.setxdirection(-1);
top.setX(top.getX()+top.getxdirection());
}
if((top.getY()<780) && (top.getY()>0)){
top.setY(top.getY()+top.getydirection());
}
if((top.getY()==780) || (top.getY()==0)){
top.setydirection(-1);
top.setY(top.getY()+top.getydirection());
}
Rect BallBounds = getBoundsBall();
Rect ScratchBounds = getBoundsScratch();
if( BallBounds.intersect(ScratchBounds) ){
top.setydirection(-1);
//top.setY(top.getY()); !!!!
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
_thread.setRunning(true);
_thread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
// simply copied from sample application LunarLander:
// we have to tell thread to shut down & wait for it to finish, or else
// it might touch the Surface after we return and explode
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
@Override
public void onDraw(Canvas canvas) {
_scratch = BitmapFactory.decodeResource(getResources(), R.drawable.red2);
ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(_scratch, raket.getX() - (_scratch.getWidth() / 2), raket.getY() - (_scratch.getHeight() / 2), null);
canvas.drawBitmap(ball, top.getX() - (ball.getWidth() / 2), top.getY() - (ball.getHeight() / 2), null);
}
}
Ball.java
public class Ball {
private int x=100; // the X coordinate
private int y=100; // the Y coordinate
int xdirection=10;
int ydirection=10;
public Ball() {
// TODO Auto-generated constructor stub
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public void setX(int a){
x=a;
}
public void setY(int b){
y=b;
}
public int getxdirection(){
return xdirection;
}
public int getydirection(){
return ydirection;
}
public void setxdirection(int a){
xdirection=xdirection*a;
}
public void setydirection(int b){
ydirection=ydirection*b;
}
//while ile ball u hareket ettir
//ball un koordinatlarını sakla
}
Scratch.java
public class Scratch {
private int x = 250;
private int y = 600;
public Scratch() {
// TODO Auto-generated constructor stub
}
public void setX(int a){
x=a;
}
public void setY(int b){
y=b;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
}
答案 0 :(得分:1)
您遗漏了以下内容:
Ball top = new Ball();
即。变量top
未初始化,但在update
方法中使用。
此外,初始化ball
的{{1}}对象 outsite 。谁知道onDraw
之前是否会调用update
?
答案 1 :(得分:1)
添加@Rudolph的答案,您确定Bitmap ball
已初始化吗?确保,请尝试将ball
记录为:
public Rect getBoundsBall(){
log.v("ball",""+ball);
return new Rect (top.getX(), top.getY(),top.getX()+ball.getWidth() ,top.getY()+ball.getHeight() );
}
ball
或top
是罪魁祸首。找出哪一个未初始化并相应地解决它。