最后一课的计算器小程序,跟着老师做的,虽然没报错,但是为什么一直运行不了呢?
package com.example.cacu;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
private Button tab1=(Button) findViewById(R.id.one);
private Button tab2=(Button) findViewById(R.id.two);
private Button tab3=(Button) findViewById(R.id.three);
private Button tab4=(Button) findViewById(R.id.four);
private Button tab5=(Button) findViewById(R.id.five);
private Button tab6=(Button) findViewById(R.id.six);
private Button tab7=(Button) findViewById(R.id.seven);
private Button tab8=(Button) findViewById(R.id.eight);
private Button tab9=(Button) findViewById(R.id.nine);
private Button tab0=(Button) findViewById(R.id.zero);
private Button tabC=(Button) findViewById(R.id.clear);
private Button tabPlus=(Button) findViewById(R.id.plus);
private Button tabMinus=(Button) findViewById(R.id.minus);
private Button tabMult=(Button) findViewById(R.id.multiply);
private Button tabDevi=(Button) findViewById(R.id.devide);
private Button tabPoint=(Button) findViewById(R.id.point);
private Button tabEqual=(Button) findViewById(R.id.equal);
private EditText etInput=(EditText) findViewById(R.id.editText1);
//Boolean clearFlag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tab0.setOnClickListener(this);
tab1.setOnClickListener(this);
tab2.setOnClickListener(this);
tab3.setOnClickListener(this);
tab4.setOnClickListener(this);
tab5.setOnClickListener(this);
tab6.setOnClickListener(this);
tab7.setOnClickListener(this);
tab8.setOnClickListener(this);
tab9.setOnClickListener(this);
tabC.setOnClickListener(this);
tabPoint.setOnClickListener(this);
tabPlus.setOnClickListener(this);
tabMinus.setOnClickListener(this);
tabMult.setOnClickListener(this);
tabDevi.setOnClickListener(this);
tabPoint.setOnClickListener(this);
tabEqual.setOnClickListener(this);
//setOnClick();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String textShow="";
//取得输入内容
textShow=etInput.getText().toString();
switch(v.getId())
{
case R.id.zero:
case R.id.one:
case R.id.two:
case R.id.three:
case R.id.four:
case R.id.five:
case R.id.six:
case R.id.seven:
case R.id.eight:
case R.id.nine:
case R.id.point:
//if(clearFlag){
// clearFlag=false;
// etInput.setText("");
//}
etInput.setText(textShow+((Button)v).getText().toString());
break;
case R.id.plus:
case R.id.minus:
case R.id.multiply:
case R.id.devide:
//if(clearFlag){
// clearFlag=false;
// etInput.setText("");
//}
etInput.setText(textShow+" "+((Button)v).getText().toString()+" ");
break;
case R.id.clear:
//clearFlag=false;
etInput.setText(" ");
break;
case R.id.equal:
getResult();
break;
}
}
//计算函数
public void getResult(){
double result=0;
String exp=etInput.getText().toString();
if(exp==null||exp.equals("")){return;}
if(!exp.contains(" ")){return;}
//if(clearFlag){
// clearFlag=false;
// return;
//}
//clearFlag=true;
String s1=exp.substring(0, exp.indexOf(" "));//运算符前面的字符串
String op=exp.substring(exp.indexOf(" ")+1, exp.indexOf(" ")+2);//运算符
String s2=exp.substring(exp.indexOf(" ")+3);//运算符后面的字符串
if(!s1.equals("")&&!s2.equals("")){
double d1=Double.parseDouble(s1);
double d2=Double.parseDouble(s2);
if(op.equals("+")){
result=d1+d2;
}else if(op.equals("-")){
result=d1-d2;
}else if(op.equals("*")){
result=d1*d2;
}else if(op.equals("/")){
if(d2==0){result=0; }
else result=d1/d2;
}
if(!s1.contains(".")&&!s2.contains(".")){
int r=(int)result;
etInput.setText(r+"");
}else{
etInput.setText(result+"");
}
}else if(!s1.equals("")&&s2.equals("")){
etInput.setText(exp);
}else if(s1.equals("")&&!s2.equals("")){
double d2=Double.parseDouble(s2);
if(op.equals("+")){
result=0+d2;
}else if(op.equals("-")){
result=0-d2;
}else if(op.equals("*")){
result=0;
}else if(op.equals("/")){
if(d2==0){result=0; }
else result=0;
}
if(!s2.contains(".")){
int r=(int)result;
etInput.setText(r+"");
}else{
etInput.setText(result+"");
}
}else {
etInput.setText("");
}
}
}