简体   繁体   中英

Why Can't i update a variable of class from another class in android(Java)?

I want to update this num variable from different classes through this Methods . but it show '0' when i display it

public class ResultNum extends AppCompatActivity{

private  int num;


public int ResultNum1(){

   return num;
}


public void yesMethod(int i){

    num = i+num;

}

Here is Another class

 yes.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            result.yesMethod(2);


        }
    });

and also I've some other classes as well which passes different numbers

here's code for displaying number

 TextView t = (TextView) findViewById(R.id.text_number);
   int x = result.ResultNum1();
    t.setText(""+x);

i also tried

 TextView t = (TextView) findViewById(R.id.text_number);
    t.setText(""+result.ResultNum1());

so What exactly i need to do now?

如前所述,您应该将int num声明为static或者确保跨应用程序的所有方法都使用相同的ResultNum类实例。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM