Right away you can see two errors in your code.
The first would refer to }
inside onPause()
, that }
should be before setting the method to close the previous method, fixing that part your code would be like this:
@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);
} //A chave fica aqui
protected void onPause() {
super.onPause();
game.cancela();
//Não aqui
}
Another error would be the variable scope game
as it is created within the method onCreate()
, only will exist within it, it is not possible to access or modify outside this method, you must declare it in a larger scope.
The whole code goes like this:
package com.example.lorenzo.pulo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
public class MainActivity extends AppCompatActivity {
//Game existe dentro do objeto, não é visível fora dele
private Game game = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout container = (FrameLayout) findViewById(R.id.container);
//Define o valor
this.game = new Game(this);
container.addView(game);
}
protected void onPause() {
super.onPause();
//Como o game está declarado em um escopo maior, não haverá problemas nessa parte
this.game.cancela();
}
}
Obs: Note the this
in front of the variable, the this
refers to the current class, would be the same as Mainactivity.game, can write without, but is useful in cases where there may be ambiguity
In "game.cancela();" the word game is in red .
– J.mateus
Knowing that I have already created the method cancels.
– J.mateus
If you put "Game game = new Game (this); " above @Override error when emulated.
– J.mateus