[英]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 . 我想通过这个方法从不同的类更新这个num变量。 but it show '0' when i display it
但是当我显示它时它显示'0'
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
类实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.