androidmonitor中没有错误仍然在按钮点击时崩溃

时间:2017-06-02 19:00:42

标签: android

我尝试搜索错误但在Android监视器中找不到任何内容。 当我点击任何按钮时,它表示应用程序无法正常工作。 Android监视器也没有生成错误。

代码: 公共类MainActivity扩展了AppCompatActivity {

Button btn_dot,btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btn_add, btn_sub, btn_mul, btn_equals, btn_div, btn_del;
Double total1 = 0.0, total2 = 0.0;
EditText et1,tv;
char op = '0';



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = (EditText) findViewById(R.id.tv);
    btn1 = (Button) findViewById(R.id.Button1);
    btn2 = (Button) findViewById(R.id.Button2);
    btn3 = (Button) findViewById(R.id.Button3);
    btn4 = (Button) findViewById(R.id.Button4);
    btn5 = (Button) findViewById(R.id.Button5);
    btn6 = (Button) findViewById(R.id.Button6);
    btn7 = (Button) findViewById(R.id.Button7);
    btn8 = (Button) findViewById(R.id.Button8);
    btn9 = (Button) findViewById(R.id.Button9);
    btn0 = (Button) findViewById(R.id.Button0);
    btn_add = (Button) findViewById(R.id.Button_add);
    btn_sub = (Button) findViewById(R.id.Button_sub);
    btn_mul = (Button) findViewById(R.id.Button_mul);
    btn_div = (Button) findViewById(R.id.Button_divide);
    btn_equals = (Button) findViewById(R.id.Button_equals);
    btn_del = (Button) findViewById(R.id.Button_DEL);
    btn_dot = (Button) findViewById(R.id.Button_dot);


    btn0.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            CharSequence val=et1.getText()+"0";
            et1.setText(val);
            tv.setText(val);
        }
    });


    btn1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            CharSequence val=et1.getText()+"1";
            et1.setText(val);
            tv.setText(val);
        }
    });


    btn2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            CharSequence val=et1.getText()+"2";
            et1.setText(val);
            tv.setText(val);
        }
    });


    btn3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            CharSequence val=et1.getText()+"3";
            et1.setText(val);
            tv.setText(val);
        }
    });


    btn4.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            CharSequence val=et1.getText()+"4";
            et1.setText(val);
            tv.setText(val);
        }
    });


    btn5.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            CharSequence val=et1.getText()+"5";
            et1.setText(val);
            tv.setText(val);
        }
    });

    btn6.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            CharSequence val=et1.getText()+"6";
            et1.setText(val);
            tv.setText(val);
        }
    });


    btn7.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            CharSequence val=et1.getText()+"7";
            et1.setText(val);
            tv.setText(val);
        }
    });


    btn8.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            CharSequence val=et1.getText()+"8";
            et1.setText(val);
            tv.setText(val);
        }
    });


    btn9.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            CharSequence val=et1.getText()+"9";
            et1.setText(val);
            tv.setText(val);
        }
    });

    btn_dot.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            CharSequence val=et1.getText()+".";
            et1.setText(val);
            tv.setText(val);
        }
    });

    btn_add.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            total1 = Double.parseDouble(et1.getText() + "");
            et1.setText(null);
            et1.setText('+');
            tv.append("+");
            op = '+';
            et1.setText(null);
            // OpCalculation();
        }
    });

    btn_sub.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            total1 = Double.parseDouble(et1.getText() + "");
            et1.setText(null);
            et1.append("-");
            tv.append("-");
            op = '-';
            et1.setText(null);
            //  OpCalculation();
        }
    });

    btn_mul.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            total1 = Double.parseDouble(et1.getText() + "");
            et1.setText(null);
            et1.append("*");
            tv.append("*");
            op = '*';
            et1.setText(null);
            //  OpCalculation();
        }
    });

    btn_div.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            total1 = Double.parseDouble(et1.getText() + "");
            et1.setText(null);
            et1.append("/");
            tv.append("/");
            op = '/';
            et1.setText(null);
            //   OpCalculation();
        }
    });

    btn_equals.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            total2 = Double.parseDouble(et1.getText() + "");
            et1.setText(null);
            if (op == '+') {
                total1 += total2;
                String t1=String.valueOf(total1);
                tv.setText(t1);
                op='0';}
            if (op == '-') {
                total1 -= total2;
                String t1=String.valueOf(total1);
                tv.setText(t1);
                op='0';}
            if (op == '*') {
                total1 *= total2;
                String t1=String.valueOf(total1);
                tv.setText(t1);
                op='0';}
            if (op == '/') {
                total1 /= total2;
                String t1=String.valueOf(total1);
                tv.setText(t1);
                op='0';}

        }
    });

    btn_del.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            et1.setText("0");
            tv.setText("0");
            total1 = 0.0;
            total2 = 0.0;
        }
    });

}

}

2 个答案:

答案 0 :(得分:0)

您的应用可能会崩溃:

total1 = Double.parseDouble(et1.getText() + "");

当您使用运算符解析整个字符串时,您必须使用此

et1.getText().toString();

希望这有帮助。

答案 1 :(得分:0)

在onCreate中初始化你的EditText et1,tv然后在onclick中改变这种格式

        String val=et1.getText().toString()+"0";
        et1.setText(val);
        tv.setText(val);