2012-01-22 14 views
17

Sto realizzando un gioco 2D in Android con Cocos2D, scritto in Java. Ecco il mio codice per la roba principale:Perché ricevo una InvocationTargetException? Gioco Android 2D

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; 
} 

Come si vede, questa funzione sembra proprio bene, ed i rettangoli in canExecuteMovement sono perfettamente bene troppo, però in questa linea:

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

Sono ottenere una InvocationTargetException. Ecco il 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) 

Quale potrebbe essere il problema? la classe getBounds() in questo tipo è:

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

Che cos'è la riga 107 nel programma GameLayer.java? – kosa

risposta

45

InvocationTargetException è solo un wrapper per un'eccezione che è gettato all'interno di un'invocazione dinamica. Il vero problema è la NullPointerException che si tratta di avvolgimento:

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

Come hai sottolineato, questa è la linea incriminata:

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

L'unico posto un puntatore nullo potrebbe accadere su questa linea è allo platform[i].getBounds(). O platform stesso è null oppure l'elemento è platform[i].

+3

Per tutti quelli che passano puoi vedere la _cause_di questo da 'e.getCause()' – djthoms