我正在尝试为 android 制作一个计数器应用程序。它有 5 个用于显示数字的 TextView、一个计数器的图片和 3 个按钮。数字加 1 按钮、数字减 1 按钮和复位按钮。当我启动应用程序时,我立即收到应用程序意外停止的消息。我正在为 java 文件添加我的代码。有人能帮助我吗?
计数器.java:
public class Counter extends Activity{
public int num=0;
public TextView txt1,txt2,txt3,txt4,txt5;
Button btnPlus, btnMinus, btnReset;
public void addOne(){
num++;
if(num >= 100000)
num = 0;
}
public void redOne(){
num--;
if(num <= -1)
num = 0;
}
public void reset(){
num = 0;
}
public void build(){
txt1.setText(num%10);
txt2.setText(num/10%10);
txt3.setText(num/100%10);
txt4.setText(num/1000%10);
txt5.setText(num/10000%10);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_counter);
txt1 = (TextView) findViewById(R.id.num1);
txt2 = (TextView) findViewById(R.id.num2);
txt3 = (TextView) findViewById(R.id.num3);
txt4 = (TextView) findViewById(R.id.num4);
txt5 = (TextView) findViewById(R.id.num5);
build();
btnPlus = (Button) findViewById(R.id.plus);
btnMinus = (Button) findViewById(R.id.minus);
btnReset = (Button) findViewById(R.id.reset);
btnPlus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addOne();
build();
}
});
btnMinus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
redOne();
build();
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
reset();
build();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_counter, menu);
return true;
}
}