-2

I have MainActivity.class that has some methods. Here is the method I want to call when the app starts:

public void loadGame(View view) {
    SharedPreferences loadGame = getSharedPreferences(PREFS, MODE_PRIVATE);         
    cashView.setText("cash: " + String.valueOf(cash));
    levelView.setText("level: " + String.valueOf(level));
}

So I try to type in the following in my onCreate method:

loadGame();

But I get an error: (View) in MainActivity cannot be applied to ()

Sorry for such a silly question but I don't get it. I thought that onCreate and other activity methods are similar to main method in Java (but they are not), but still I can easily assign a method for Button onClick. Why can't I call a method when the app starts? Any solution?

upd: MainActivity code:

public class MainActivity extends AppCompatActivity {
    private int cash = 1000;
    private int level = 1;
    private TextView cashView;
    private TextView levelView;

    public static final String PREFS = "MySavedGameFile";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

     //the compiler says it cannot resolve symbol 'view'
     //  loadGame(view);
     //I need to call the method anyway
        cashView = (TextView) findViewById(R.id.cashView);
        levelView = (TextView) findViewById(R.id.levelView);

    }
     public void loadGame(View view) {
        SharedPreferences loadGame = getSharedPreferences(PREFS, MODE_PRIVATE);
        cash = loadGame.getInt("savedCash", 1000);
        level = loadGame.getInt("savedLevel", 1);
        cashView.setText("Cash: " + String.valueOf(cash));
        levelView.setText("Level: " + String.valueOf(level));
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97

2 Answers2

1

The problem your code is that you are defining a method with a signature but using another for calling it..

if you define a method public void loadGame(View view) then you need to call it like loadGame(view)

if you do something like loadGame(); then the compiler is going crazy because he can not find that method (a method without parameters...)

Quickfix:

replace add the view(whatever it is) to the method call

Update:

you can replace the method public void loadGame(View view) { for this public void loadGame( ) { because you are not using any view in that method and in the oncreate there is no view (except for the controls cashView and levelView but those are Activity fields... ) declared.


Your final code should look like:

 ...
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        cashView = (TextView) findViewById(R.id.cashView);
        levelView = (TextView) findViewById(R.id.levelView);
        loadGame( );

    }
     public void loadGame() {
        SharedPreferences loadGame = getSharedPreferences(PREFS, MODE_PRIVATE);
        cash = loadGame.getInt("savedCash", 1000);
        level = loadGame.getInt("savedLevel", 1);
        cashView.setText("Cash: " + String.valueOf(cash));
        levelView.setText("Level: " + String.valueOf(level));
      }
Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

You need to pass a View object as an argument to the loadGame() method. Considering you do not use view in your method, instead you can just remove it as a parameter, or create a new method of the same name with no parameters.

vontell
  • 342
  • 3
  • 17