为什么我会收到一个的InvocationTargetException? Android的2D游戏 [英] Why am I getting an InvocationTargetException? Android 2D game

查看:164
本文介绍了为什么我会收到一个的InvocationTargetException? Android的2D游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个2D游戏在Android中使用Cocos2D中,用Java编写的。这是我的code的主要的东西:

 公共无效gameLoop(浮点DT){
    //播放器重力
    如果(canExecuteMovement(0,6)){
        guy.moveY(6);
    }

    //播放器运动
    如果(方向== 1){
        如果(canExecuteMovement(-3,0))
            guy.moveX(-3);
    }否则,如果(方向== 2){
        如果(canExecuteMovement(3,0))
            guy.moveX(3);
    }
}

私人布尔canExecuteMovement(INT的xChange,诠释yChange){
    INT projectedX = guy.getBounds()左+的xChange。
    INT projectedY = guy.getBounds()上衣+ yChange。
    Log.i(DD,帅哥:+ guy.getBounds()的toString());
    矩形projectedBounds =新的矩形(projectedX,projectedY,projectedX + guy.getWidth(),projectedY + guy.getHeight());
    Log.i(DD,帅哥:+ projectedBounds.toString());
    的for(int i = 0; I< platformCount;我++){
        如果(Rect.intersects(projectedBounds,平台[I] .getBounds())){
            返回false;
        }
    }

    返回true;
}
 

正如你看到的,这个功能看起来蛮好的,在canExecuteMovement矩形是完全没关系,但是在这一行:

 第107行:如果(Rect.intersects(projectedBounds,平台[I] .getBounds())){
 

我得到一个的InvocationTargetException。这里是logcat的:

  01-21 23:10:12.601:W / System.err的(13118):java.lang.reflect.InvocationTargetException
01-21 23:10:12.601:W / System.err的(13118):在java.lang.reflect.Method.invokeNative(本机方法)
01-21 23:10:12.605:W / System.err的(13118):在java.lang.reflect.Method.invoke(Method.java:511)
01-21 23:10:12.605:W / System.err的(13118):在org.cocos2d.actions.CCTimer.update(CCTimer.java:82)
01-21 23:10:12.605:W / System.err的(13118):在org.cocos2d.actions.CCScheduler.tick(CCScheduler.java:253)
01-21 23:10:12.605:W / System.err的(13118):在org.cocos2d.nodes.CCDirector.drawCCScene(CCDirector.java:679)
01-21 23:10:12.605:W / System.err的(13118):在org.cocos2d.nodes.CCDirector.onDrawFrame(CCDirector.java:649)
01-21 23:10:12.605:W / System.err的(13118):在android.opengl.GLSurfaceView $ GLThread.guardedRun(GLSurfaceView.java:1462)
01-21 23:10:12.605:W / System.err的(13118):在android.opengl.GLSurfaceView $ GLThread.run(GLSurfaceView.java:1216)
01-21 23:10:12.605:W / System.err的(13118):显示java.lang.NullPointerException:产生的原因
01-21 23:10:12.608:W / System.err的(13118):在com.qasim.platformer.GameLayer.canExecuteMovement(GameLayer.java:107)
01-21 23:10:12.608:W / System.err的(13118):在com.qasim.platformer.GameLayer.gameLoop(GameLayer.java:86)
01-21 23:10:12.608:W / System.err的(13118):8 ...更多
01-21 23:10:12.620:D / dalvikvm(13118):GC_CONCURRENT释放460K,6%免费9279K / 9863K,暂停2MS + 3ms的
01-21 23:10:12.624:I / DD(13118):家伙:矩形(252,63  -  300,111)
 

可能是什么问题?的的getBounds()中的家伙类是这样的:

 公共矩形的getBounds(){
    返回新的矩形(X,Y,X +宽度,Y +高);
}
 

解决方案

<一个href="http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/InvocationTargetException.html"><$c$c>InvocationTargetException只是一个包装,它是一个动态调用内抛出异常。真正的问题是<一个href="http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html"><$c$c>NullPointerException ,它的包装:

 产生的原因:显示java.lang.NullPointerException
  在com.qasim.platformer.GameLayer.canExecuteMovement(GameLayer.java:107)
  在com.qasim.platformer.GameLayer.gameLoop(GameLayer.java:86)
 

正如您所指出的那样,这是有问题的行:

 如果(Rect.intersects(projectedBounds,平台[I] .getBounds())){
 

一个空指针可能会发生在这条线的地方是在平台[I] .getBounds()。无论是平台自身平台元素[I]

I am making a 2D game in Android with Cocos2D, written in Java. Here is my code for the main stuff:

public void gameLoop(float dt) {
    //Player Gravity
    if(canExecuteMovement(0, 6)) {
        guy.moveY(6);
    }

    //Player Movement
    if(direction == 1) {
        if(canExecuteMovement(-3, 0))
            guy.moveX(-3);
    } else if(direction == 2) {
        if(canExecuteMovement(3, 0))
            guy.moveX(3);
    }
}

private boolean canExecuteMovement(int xChange, int yChange) {
    int projectedX = guy.getBounds().left + xChange;
    int projectedY = guy.getBounds().top + yChange;
    Log.i("DD", "guy:" + guy.getBounds().toString());
    Rect projectedBounds = new Rect(projectedX, projectedY, projectedX + guy.getWidth(), projectedY + guy.getHeight());
    Log.i("DD", "guy:" + projectedBounds.toString());
    for (int i = 0; i < platformCount; i++) {
        if (Rect.intersects(projectedBounds, platform[i].getBounds())) {
            return false;
        }
    }

    return true;
}

As you see, this function looks just fine, and the rectangles in canExecuteMovement are perfectly fine too, however in this line:

LINE 107: if (Rect.intersects(projectedBounds, platform[i].getBounds())) {

I am getting a InvocationTargetException. Here is the logcat:

01-21 23:10:12.601: W/System.err(13118): java.lang.reflect.InvocationTargetException
01-21 23:10:12.601: W/System.err(13118):    at java.lang.reflect.Method.invokeNative(Native Method)
01-21 23:10:12.605: W/System.err(13118):    at java.lang.reflect.Method.invoke(Method.java:511)
01-21 23:10:12.605: W/System.err(13118):    at org.cocos2d.actions.CCTimer.update(CCTimer.java:82)
01-21 23:10:12.605: W/System.err(13118):    at org.cocos2d.actions.CCScheduler.tick(CCScheduler.java:253)
01-21 23:10:12.605: W/System.err(13118):    at org.cocos2d.nodes.CCDirector.drawCCScene(CCDirector.java:679)
01-21 23:10:12.605: W/System.err(13118):    at org.cocos2d.nodes.CCDirector.onDrawFrame(CCDirector.java:649)
01-21 23:10:12.605: W/System.err(13118):    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1462)
01-21 23:10:12.605: W/System.err(13118):    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1216)
01-21 23:10:12.605: W/System.err(13118): Caused by: java.lang.NullPointerException
01-21 23:10:12.608: W/System.err(13118):    at com.qasim.platformer.GameLayer.canExecuteMovement(GameLayer.java:107)
01-21 23:10:12.608: W/System.err(13118):    at com.qasim.platformer.GameLayer.gameLoop(GameLayer.java:86)
01-21 23:10:12.608: W/System.err(13118):    ... 8 more
01-21 23:10:12.620: D/dalvikvm(13118): GC_CONCURRENT freed 460K, 6% free 9279K/9863K, paused 2ms+3ms
01-21 23:10:12.624: I/DD(13118): guy:Rect(252, 63 - 300, 111)

What could be the problem? the getBounds() class in guy is this:

public Rect getBounds() {
    return new Rect(x, y, x+width, y+height);
}

解决方案

InvocationTargetException is just a wrapper for an exception that's thrown within a dynamic invocation. The true problem is the NullPointerException that it's wrapping:

Caused by: java.lang.NullPointerException
  at com.qasim.platformer.GameLayer.canExecuteMovement(GameLayer.java:107)
  at com.qasim.platformer.GameLayer.gameLoop(GameLayer.java:86)

As you've pointed out, this is the offending line:

if (Rect.intersects(projectedBounds, platform[i].getBounds())) {

The only place a null pointer could be happening on this line is at platform[i].getBounds(). Either platform itself is null, or the element at platform[i] is.

这篇关于为什么我会收到一个的InvocationTargetException? Android的2D游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆