Felipe,
Ao executar o Jumper, apresentou o seguinte erro:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: vlc.com.br.jumper, PID: 2508
java.lang.RuntimeException: Unable to resume activity {vlc.com.br.jumper/vlc.com.br.jumper.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void vlc.com.br.jumper.engine.Game.inicia()' on a null object reference
Seguem a MainActivity e a Game.
package vlc.com.br.jumper;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
import vlc.com.br.jumper.engine.Game;
public class MainActivity extends Activity {
private Game game;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout container = (FrameLayout) findViewById(R.id.container);
Game game = new Game(this);
container.addView(game);
}
@Override
protected void onResume() {
super.onResume();
game.inicia();
new Thread(game).start();
}
@Override
protected void onPause() {
super.onPause();
game.pausa();
}
}
package vlc.com.br.jumper.engine;
import android.content.Context;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import vlc.com.br.jumper.elements.Passaro;
/**
* Created by Valeria on 01/12/2016.
*/
public class Game extends SurfaceView implements Runnable {
private boolean isRunning = true;
private SurfaceHolder holder = getHolder();
private Passaro passaro;
public Game(Context context) {
super(context);
inicializaElementos();
}
private void inicializaElementos() {
passaro = new Passaro();
}
@Override
public void run() {
while(isRunning) {
//Neste loop vamos gerenciar os elementos do Jumper.
if(!holder.getSurface().isValid()) continue;
Canvas canvas = holder.lockCanvas();
passaro.desenhaNo(canvas);
passaro.cai();
holder.unlockCanvasAndPost(canvas);
}
}
public void inicia() {
this.isRunning = true;
}
public void pausa() {
this.isRunning = false;
}
}