Programming with android studio

Asked

Viewed 57 times

-1

I am having error in my project, the error that appears is the following:

->/ 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();

        }
    }
}
  • In "game.cancela();" the word game is in red .

  • Knowing that I have already created the method cancels.

  • If you put "Game game = new Game (this); " above @Override error when emulated.

1 answer

3

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.