jason flanagan
jason flanagan

Reputation: 117

app forces close every time i open a new class

i have an app that has a int called Money and i .putExtra it to go to another class but every time i push the button to go to that class it forces close the app.

here is the code i used to send the int

 int Money = 0;

 GoToSettings.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
                Intent intent = new Intent(getApplicationContext(), settings.class);
                intent.putExtra("money",Money);
                startActivity(intent);

        }

    });

and here is where i receive it and try to save it

public class settings extends Activity {

int LoadDef = 0;
Button Save,Load,ClsSave;

Intent intent = getIntent();
int Money = intent.getIntExtra("money", 0);

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

    Save = (Button) findViewById(R.id.Save);
    Load = (Button) findViewById(R.id.Load);
    ClsSave = (Button) findViewById(R.id.ClsSave);


                Save.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub

                            SharedPreferences sharedPreferences=getSharedPreferences("MoneySave", Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor=sharedPreferences.edit();
                            editor.putInt("MoneySave", Money);
                            editor.commit();

                        }
                    });

could someone help me fine out why it forces close?

Upvotes: 0

Views: 41

Answers (1)

Arash GM
Arash GM

Reputation: 10395

The problem is here :

Intent intent = getIntent();

you should get extra from your intent , in onCreate() not in class definition

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

   Intent intent = getIntent();
   int Money = intent.getIntExtra("money", 0);

Upvotes: 2

Related Questions