0

sorry I'm really Amateur. i have 4 class: A B C D. I want to calculate something in my Class C and show the result in Class D but i have no idea why my app always show Zero as result in Class D!!!

Here is brief of my codes:

Class C:

public class ClassC extends Activity {

            public static int Result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ClassC);

                    final CheckBox cb = (CheckBox) findViewById(R.id.CheckBox);

                         Button b1 = (Button) findViewById(R.id.Calculate);
                 b1.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {

                            boolean b = cb.isChecked();

                            if (ClassA.MyVazn() < 251 ){
            Result = 26500;
            }
            else if (ClassA.MyVazn() >= 251 && ClassA.MyVazn() <= 500){
            Result = 31800;
            }

            else {
            Result = 38700;
                            }

                            if (b){
                            Result += 12000;
                            }

                            Intent a = new Intent (ClassC.this,ClassD.class);
            startActivity(a);
            overridePendingTransition(R.anim.a,R.anim.b);

                        }
            }

            public static int FinalResult (){
        return Result;
    }




}

Class D

public class ClassD extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ClassD);


        TextView r = (TextView) findViewById(R.id.Result);
        r.setText(String.valueOf(ClassC.FinalResult()));
      }
}

help plz :(

Saeed74
  • 65
  • 1
  • 10
  • use bundle to send data from one activity to another. here's what the [link](http://stackoverflow.com/questions/14876273/simple-example-for-intent-and-bundle) for what you have to do – rutulPatel Apr 23 '14 at 20:39

3 Answers3

0

It looks like Result is not calculated in ClassC until something is clicked, but you are trying to read it in ClassD's onCreate method, before the calculation has occurred.

You probably need to move the following line to some part of your code that runs after the click.

    r.setText(String.valueOf(ClassC.FinalResult()));
merlin2011
  • 71,677
  • 44
  • 195
  • 329
0

because class C probably does not exist anymore. send the result in the intent

Intent a = new Intent (ClassC.this,ClassD.class);
a.putExtra("result",result);
startActivity(a);

then in your ClassD get it

Intent i = getIntent();
int resultFromActivity = i.getIntExtra("result",someDefaultValue);

TextView r = (TextView) findViewById(R.id.Result);
r.setText(String.valueOf(resultFromActivity));
tyczj
  • 71,600
  • 54
  • 194
  • 296
0

thanks to all but i has found my problem, the code that is here is all Ok , and problem caused for my ClassB that never compiled because Defined some code to skip it in some special condition! and then i tried to get value of my methods in ClassB!!

Saeed74
  • 65
  • 1
  • 10