0

I can't believe I have so much trouble with this. I have this variable in my game activity:

public static int numberOfPointsA;

and in another activity

public static int numberOfPointsB;

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:

public static int totalScore = ClassA.numberOfPointsA + ClassB.numberOfPointsB;

textView.setText("" + totalScore);

But that's not working. So I tried with intent:

In game classA:

Intent intent = new Intent(this, Menu.class);
                intent.putExtra("foobar", numberOfPointsA);
                startActivity(intent);

and in menu class:

Intent intent = getIntent();
    int numberOfPointsA = intent.getIntExtra("foobar", 0);

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{

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;

@Override
protected 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() {

    naslov = (TextView) findViewById(R.id.tvIzborNaslov);
    toploHladno = (Button) findViewById(R.id.bIzbor1);
    asocijacije = (Button) findViewById(R.id.bIzbor2);
    cigle = (Button) findViewById(R.id.bIzbor3);
    spojnice = (Button) findViewById(R.id.bIzbor4);
    nazad = (Button) findViewById(R.id.bIzborNazad);
    poeniTH = (Button) findViewById(R.id.bPoeniTH);
    poeniAso = (Button) findViewById(R.id.bPoeniAso);
    poeniCigle = (Button) findViewById(R.id.bPoeniCigle);
    poeniSpojnice = (Button) findViewById(R.id.bPoeniSpojnice);
    poeniUkupno = (Button) findViewById(R.id.bPoeniUkupno);


    toploHladno.setOnClickListener(this);
    asocijacije.setOnClickListener(this);
    cigle.setOnClickListener(this);
    spojnice.setOnClickListener(this);
    nazad.setOnClickListener(this);

}


@Override
protected 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;
    }

}

}

marjanbaz
  • 1,052
  • 3
  • 18
  • 35
  • did you mean pass value from one activity to another activity? This may help: http://stackoverflow.com/questions/3510649/how-to-pass-a-value-from-one-activity-to-another-in-android – oentoro Apr 07 '13 at 01:38
  • Yes, I have some score, for example 195, and I set it to my public static int variable and I need to send it to another activity. This link you gave me...I tried that also. I also get error as soon I start my activity. If I place bundle in onCreate method, I can't use int variable anymore. – marjanbaz Apr 07 '13 at 01:47
  • 1
    You have an NPE at `Izbor.java:22` according to the log... I'd look there ;) – Steven Byle Apr 07 '13 at 02:00
  • I have a feeling you're using `getIntent()` in a constructor. Don't do that. (It belongs in `onCreate()`.) Either way, Steven has the right idea. – Cat Apr 07 '13 at 02:01
  • I tried in onCreate method, I get the same error. NPE is on line where bundle is. – marjanbaz Apr 07 '13 at 02:03
  • Did `startActivityForResult()` not work for you? – codeMagic Apr 07 '13 at 02:55
  • No, it didn't. But this is actually something else. – marjanbaz Apr 07 '13 at 11:23

2 Answers2

0

The following is a very basic example, using two activities to pass an int around.

First Activity, which you start from:

public class FirstActivity extends Activity {
    private int mTotal = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        //.... onCreate code, layout stuff etc....    
    }

    // this is tied to a View onClick call
    public void moveToSecondAct(View view) {
        Intent intent = new Intent();
        intent.putExtra("RUNNING_TOTAL",mTotal);
        intent.setClass(this, SecondActivity.class);
        //... any other options like ...
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        // start the second Activity
        startActivity(intent);
    }
}

And the second Activity example is something like:

public class SecondActivity extends Activity {
    private int mTotal = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        mTotal = getIntent().getIntExtra("RUNNING_TOTAL", 0);
        //.... onCreate code, layout stuff etc....

        Log.i("Running total is:"+ mTotal);
        // update the total on the TextView
        TextView tv = (TextView) findViewById(R.id.poeniUkupno);
        tv.setText("Total: "+ mTotal);
    }
}

Edit: Some changes to try help make things clearer

Tigger
  • 8,980
  • 5
  • 36
  • 40
  • I've tried your way, I don't get error now, but now I don't get final total score set to textView and I get double instance of my activity, I have to go back twice with my back button. :( – marjanbaz Apr 07 '13 at 02:24
  • Also, when I set first value to texview and then play next game, and then set the second value to the next textView, the first one lost it's value. – marjanbaz Apr 07 '13 at 02:26
  • What is the `id` of the `TextView`? – Tigger Apr 07 '13 at 02:42
  • poeniAso is for the first one, poeniTH is for the 2nd one, and poeniUkupno is for total score. – marjanbaz Apr 07 '13 at 02:45
  • I've made some changes to help get you started along the path, but the rest is up to you to work out. It is impossible for me to second guess all your code. – Tigger Apr 07 '13 at 02:50
  • I did exactly what you did and this is not working. Now after your edit, I don't have two activities on back button, but I still don't get total at all, it empty box, and after one game is finished it's value is set to the box, but after the 2nd game gets finished it value is also set to the box, but the first box is now empty. I've edited my first post and included my whole menu class. – marjanbaz Apr 07 '13 at 11:17
0

I didn't completely get what you have been trying to say but what I got was you want to create a variable in FirstActivity send its value to SecondActivity and then pass their sum to ResultActivity if its so then here you go

//First Activity
public class FirstActivity extends Activity{
    int firstValue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        firstValue = 10;
    }
    public sendValueToSecondActivity(View view){
        Intent intent = new Intent(this, SecondActivity.class);
        intent.putExtra("firstValue", firstValue);
        startActivity(intent);
    }
}

//Second Activity
public class SecondActivity extends Activity{
    int sumOfFirstAndSecond;
    int secondValue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        secondValue = 20;

    }
    public sendValueToSecondActivity(View view){
        sumOfFirstAndSecond = getIntent().getIntExtra("firstValue") + secondValue;
        Intent intent = new Intent(this, ResultActivity.class);
        intent.putExtra("sumOfFirstAndSecond", sumOfFirstAndSecond);
        startActivity(intent);
    }
}

//Result Activity
public class ResultActivity extends Activity{
    TextView textView;
    int result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        secondValue = 20;
        textView = (TextView) findViewById(R.id.textView);
    }
    public setResultToTextView(View view){
        result = getIntent().getIntExtra("sumOfFirstAndSecond");
        textView.setText(""+result);
    }
}
Sushant
  • 1,834
  • 19
  • 32