How to pass int value from one class to another?

brice07

New Member
I can't believe I have so much trouble with this. I have this variable in my game activity:\[code\]public static int numberOfPointsA;\[/code\]and in another activity\[code\]public static int numberOfPointsB;\[/code\]Now I need these values to pass to another activity, where I should total these values and set result to textView. Since these are public static variables I tried:\[code\]public static int totalScore = ClassA.numberOfPointsA + ClassB.numberOfPointsB;textView.setText("" + totalScore);\[/code\]But that's not working. So I tried with intent:In game classA:\[code\]Intent intent = new Intent(this, Menu.class); intent.putExtra("foobar", numberOfPointsA); startActivity(intent);\[/code\]and in menu class:\[code\]Intent intent = getIntent(); int numberOfPointsA = intent.getIntExtra("foobar", 0);\[/code\]But that's not working either. If I place in the scope of activity, as soon as activity starts it crashes. If I place it in onCreate method, I can's use my int variable anymore, I don't need it in onCreate method, I need it elsewhere.So how to set my variable in game class, pass to menu class, save it there and then make it wait until I finish my game class B and do the same with that variable, and then total those two variables and set it successfuly to the textView?Menu activity:public class Izbor extends Activity implements OnClickListener{\[code\]private int asocijacijeUkupno = 0;private int thUkupno = 0;int ukupanBrojPoena = asocijacijeUkupno + thUkupno;Button toploHladno, asocijacije, cigle, spojnice, nazad, poeniTH, poeniAso, poeniCigle, poeniSpojnice, poeniUkupno;TextView naslov;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); asocijacijeUkupno = getIntent().getIntExtra("RUNNING_TOTAL", 0); thUkupno = getIntent().getIntExtra("RUNNING_TOTAL2", 0); setContentView(R.layout.izbor); addListenerOnButton();}private void addListenerOnButton() { toploHladno.setOnClickListener(this); asocijacije.setOnClickListener(this); cigle.setOnClickListener(this); spojnice.setOnClickListener(this); nazad.setOnClickListener(this);}@Overrideprotected void onStart() { super.onStart(); if(asocijacijeUkupno != 0){ poeniAso.setText("" + asocijacijeUkupno); }else{ poeniAso.setText(""); } if(thUkupno != 0){ poeniTH.setText("" + thUkupno); }else{ poeniTH.setText(""); } if(ukupanBrojPoena != 0){ poeniUkupno.setText("" + ukupanBrojPoena); }else{ poeniUkupno.setText(""); }}public void onClick(View v) { switch(v.getId()){ case R.id.bIzbor1: if(music == true){ buttonClicks.start(); } startActivity(new Intent("rs.androidaplikacije.toplo_hladno.TOPLOHLADNO")); break; case R.id.bIzbor2: if(music == true){ buttonClicks.start(); } startActivity(new Intent("rs.androidaplikacije.toplo_hladno.ASOCIJACIJE")); break; case R.id.bIzbor3: if(music == true){ buttonClicks.start(); } break; case R.id.bIzbor4: if(music == true){ buttonClicks.start(); } break; case R.id.bIzborNazad: if(music == true){ buttonBack.start(); } finish(); break; }}\[/code\]}
 
Top