Bom dia,
Estou com um problema com minha class MainActivity, quando eu mando copilhar aparece uma mensagens de erro no console:
12-13 17:14:58.032 10363-10363/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.guilherme.jumper, PID: 10363
java.lang.RuntimeException: Unable to resume activity {com.example.guilherme.jumper/com.example.guilherme.jumper.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.guilherme.jumper.engine.Game.inicia()' on a null object reference
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.guilherme.jumper.engine.Game.inicia()' on a null object reference
at com.example.guilherme.jumper.MainActivity.onResume(MainActivity.java:28)
O erro esta falando que contrutor onResume este retornando null, aqui o meu codigo:
public class MainActivity extends AppCompatActivity {
public Game game;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout container = findViewById(R.id.container);
Game game = new Game(this);
container.addView(game);
}
@Override
protected void onResume() {
super.onResume();
this.game.inicia();
new Thread(game).start();
}
@Override
protected void onPause() {
super.onPause();
this.game.pausa();
}
}
e também class Game:
public class Game extends SurfaceView implements Runnable {
private boolean isRunning = true;
private SurfaceHolder holder = getHolder();
private passaro passaro_0;
public Game(Context context) {
super(context);
inicializaElementos();
}
private void inicializaElementos() {
passaro_0 = new passaro();
}
@Override
public void run() {
while(isRunning){ // desenho dos camponentes do jogo
if (!holder.getSurface().isValid()) continue;
Canvas canvas = holder.lockCanvas();
passaro_0.desenhaNo(canvas);
passaro_0.cai();
holder.unlockCanvasAndPost(canvas);
}
}
public void inicia() {
isRunning = true;
}
public void pausa() {
isRunning = false;
}
}
Lembrando que eu estou utilizando android-studio3.0.1
Como eu posso resolver este erro?