I am a beginner, so do not immediately throw bricks, but rather advise to read good literature

public class MainActivity extends Activity { private int money; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState != null) { savedInstanceState.getInt("money", money); } Money(); } @Override public void onSaveInstanceState(Bundle savedInstanceState) { savedInstanceState.putInt("money", money); } public void onMon(View v) { money++; } public void Money () { final TextView moneyView = (TextView) findViewById(R.id.money_view); int n = money; String money = String.format("%08d%n", n); moneyView.setText(money); } } 

Closed due to the fact that off-topic participants YuriySPb , Kromster , αλεχολυτ , Mikhail Vaysman , Grundy 5 Apr '17 at 14:52 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Yuriy SPb, Kromster, αλεχολυτ, Mikhail Vaysman, Grundy
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    int money is not initialized. public void onMon(View v) does not need raguments. and the name of the method is not appropriate - Senior Pomidor
  • @SeniorPomidor, this is a button click handler function. There, in any way, she accepts View. - Kirill Malyshev
  • one
    In this case, @SeniorPomidor does not need to initialize money, jvm implicitly initializes this field to zero. - Nikita Gordeev
  • 2
    @KirillMalyshev, tell me what exactly does not work, from the attached code, little is clear. I can only say right away that it is not customary in Java to write the name of the method with a capital letter (the Money () method in your example). And it’s not very good that it is not clear what kind of onMon () handler is and what it should do (called every Monday? Then you can give a more telling name to the method) - Nikita Gordeyev

1 answer 1

The savedInstanceState.getInt method returns a value by key, and the second argument is for the default value (the default value will be returned if there is no value for the "money" key). In your code, savedInstanceState.getInt returns a value to void, so change this part of the code to money = savedInstanceState.getInt("money", 0); .

Change the name of the Money() method to money() , since in Java it is not customary to call methods with a capital letter.

The code in the money() method can be rewritten and get rid of int n and String money .

 public class MainActivity extends Activity { private int money; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState != null) { money = savedInstanceState.getInt("money", 0); } money(); } @Override public void onSaveInstanceState(Bundle savedInstanceState) { savedInstanceState.putInt("money", money); } public void onMon(View v) { money++; } public void money () { TextView moneyView = (TextView) findViewById(R.id.money_view); moneyView.setText(String.format("%08d%n", money)); } } 

As for literature, I would recommend reading this book - "Brian Hardy, Bill Phillips, Android. Programming for Professionals, 2nd Edition "

  • God grant you the health of happiness, Internet speed of 1 gig, and immunity from cryptographers! - Criakl