我正在嘗試爲android生成一個計數器應用程序。它有5個用於顯示數字的TextViews,一個計數器和3個按鈕的圖片。用於將數字加1的按鈕,用於從數字和重置按鈕中減1的按鈕。在啓動應用程序時,我立即收到一條消息,指出應用程序意外停止。我正在爲java文件添加我的代碼。有人能幫助我嗎?android應用程序意外停止計數器
Counter.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;
}
}
發佈錯誤堆棧跟蹤。我想這個錯誤被零除。 – kosa