为了账号安全,请及时绑定邮箱和手机立即绑定

这个计算器的代码为什么一按键就闪退啊

这个计算器的代码为什么一按键就闪退啊

qq_PaulKylin_0 2016-07-28 15:15:44
ackage com.example.calculater;import android.app.Activity;import android.os.Bundle;import android.renderscript.Double4;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class calculater extends Activity implements OnClickListener{ private Button bt7; private Button bt8; private Button bt9; private Button bt0; private Button bt1; private Button bt2; private Button bt3; private Button bt4; private Button bt5; private Button bt6; private Button btadd; private Button btjian; private Button btmulticul; private Button btdivide; private Button btdel; private Button btclear; private Button btequal; private Button btdian; private EditText edt; private boolean cf; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.calculater); bt7=(Button) findViewById(R.id.bt_7); bt8=(Button) findViewById(R.id.bt_8); bt9=(Button) findViewById(R.id.bt_9); bt4=(Button) findViewById(R.id.bt_4); bt5=(Button) findViewById(R.id.bt_5); bt6=(Button) findViewById(R.id.bt_6); bt1=(Button) findViewById(R.id.bt_1); bt2=(Button) findViewById(R.id.bt_2); bt3=(Button) findViewById(R.id.bt_3); btadd=(Button) findViewById(R.id.bt_add); btjian=(Button) findViewById(R.id.bt_jian); btmulticul=(Button) findViewById(R.id.bt_multicul); btdivide=(Button) findViewById(R.id.bt_divide); bt0=(Button) findViewById(R.id.bt_0); btdian=(Button) findViewById(R.id.bt_dian); btequal=(Button) findViewById(R.id.bt_equal); btclear=(Button) findViewById(R.id.bt_clear); btdel=(Button) findViewById(R.id.bt_del); bt7.setOnClickListener(this); bt8.setOnClickListener(this); bt9.setOnClickListener(this); bt4.setOnClickListener(this); bt5.setOnClickListener(this); bt6.setOnClickListener(this); bt1.setOnClickListener(this); bt2.setOnClickListener(this); bt3.setOnClickListener(this); bt0.setOnClickListener(this); btclear.setOnClickListener(this); btdel.setOnClickListener(this); btadd.setOnClickListener(this); btjian.setOnClickListener(this); btmulticul.setOnClickListener(this); btdivide.setOnClickListener(this); btdian.setOnClickListener(this); btequal.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub String str=edt.getText().toString(); switch (v.getId()) { case R.id.bt_0: case R.id.bt_1: case R.id.bt_2: case R.id.bt_3: case R.id.bt_4: case R.id.bt_5: case R.id.bt_6: case R.id.bt_7: case R.id.bt_8: case R.id.bt_9: case R.id.bt_dian: if(cf) { cf=false; str=""; edt.setText(""); } edt.setText(str+((Button)v).getText()); break; case R.id.bt_add: case R.id.bt_jian: case R.id.bt_multicul: case R.id.bt_divide: if(cf) { cf=false; str=""; edt.setText(""); } edt.setText(str+" "+((Button) v).getText()+" "); break; case R.id.bt_clear: cf=false; str=""; edt.setText(""); break; case R.id.bt_del: if(cf) { cf=false; str=""; edt.setText(""); }else if(str!=null&&!str.equals("")){ edt.setText(str.substring(0, str.length()-1)); } break; case R.id.bt_equal: getResult(); break; } } private void getResult() { String exp=edt.getText().toString(); if (!exp.equals(" ")&&exp!=null)  { return; } if (exp==null||exp.equals("")) { return; } if(exp.contains("")) { return; } if(cf){ cf=false; return; } cf=true; double a=0; String sp1=exp.substring(0, exp.indexOf(" ")); String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")); String sp2=exp.substring(exp.indexOf(" ")+3); if(!sp1.equals("")&&!sp2.equals("")) { double d1=Double.parseDouble(sp1); double d2=Double.parseDouble(sp2); if(op.equals("+")) { a= d1+d2; }else if (op.equals("-")) { a=d1-d2; }else if(op.equals("×")) { a=d1*d2; }else if(op.equals("÷")) { if (d2==0) { a=0; }else { a=d1/d2; } } if (!sp1.contains(".")&&!sp2.contains(".")&&!op.equals("÷")) { int r=(int) a; edt.setText(r+""); }else { edt.setText(a+""); } }else if (!sp1.equals("")&&sp2.equals("")) { edt.setText(exp); }else if(sp1.equals("")&&!sp2.equals("")) { double d2=Double.parseDouble(sp2); if(op.equals("+")) { a= 0+d2; }else if (op.equals("-")) { a=0-d2; }else if(op.equals("×")) { a=0*d2; }else if(op.equals("÷")) { a=0; } if (!sp2.contains(".")) { int r=(int) a; edt.setText(r+""); }else { edt.setText(a+""); } }else { edt.setText(""); } }}
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 2230 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信