为什么我不能将此 int 加 1?
这有点令人沮丧,因为我不知道为什么它没有添加。我有一个公共整数(public int intVar;),还有一个我想增加1的方法。我尝试过intVar ++,intVar + = 1和intVar = intVar + 1;...
那么为什么我不能增加这个除 1 的整数?
public class activityMain extends Activity {
public int intVar;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cdtracks);
intVar = 1;
}
public void myMethod() {
intVar = intVar+1;
Log.d("MYTAG", "intVar="+intVar)
//everytime I call the method, it will always say 1.
}
}
This is a bit frustrating, because I don't know why it's not adding. I have a public integer (public int intVar;), and a method that I want to increase by 1. I've tried intVar++, intVar+=1 and intVar = intVar + 1;...
So why can't I increase this integer by 1?
public class activityMain extends Activity {
public int intVar;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cdtracks);
intVar = 1;
}
public void myMethod() {
intVar = intVar+1;
Log.d("MYTAG", "intVar="+intVar)
//everytime I call the method, it will always say 1.
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否确定您显示的 onCreate 实际上已执行?如果你调用你的方法两次会发生什么?
Did you make sure the onCreate that you show is actually executed? What happens if you call your method twice?
将日志语句添加到 OnCreate 方法(如果尚未这样做)。这可能会告诉你一些事情。您可能会发现intVar变量应该存储在savedInstanceState包中,以使其持久化。这是在活动的 onSaveInstanceState 方法中完成的。
Add a log statement to the OnCreate method, if you have not already done so. That might tell you something. You will probably find that the intVar variable should be stored in the savedInstanceState bundle, to make it persistent. That is done in the onSaveInstanceState method of the activity.