Programming with android studio [closed]

0

I'm having an error in my project, the error that appears is as follows:

  

-> / Error: (23, 31) error: ';' expected /

package com.example.lorenzo.pulo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {

    @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);


    protected void onPause() //aqui aparece esse erro de error: ';' expected   {
        super.onPause();
        game.cancela();

        }
    }
}
    
asked by anonymous 05.08.2017 / 20:11

1 answer

3

You can see two errors in your code right away.

The first would be referring to } within onPause() , this } should be before the method definition to close the previous method, correcting that part your code would look 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 scope of the game variable as it is created within the onCreate() method, it will only exist inside it, it can not access or modify outside of this method, it should declare it in a larger scope

The complete code looks 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();
    }
}
Note the this in front of the variable, 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

    
05.08.2017 / 22:00