androidmonitor应用程序在单击按钮时仍会崩溃,没有错误

androidmonitor应用程序在单击按钮时仍会崩溃,没有错误,android,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; Doub

我尝试搜索错误,但在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;
        }
    });

}

}您的应用程序可能会在此处崩溃:

total1 = Double.parseDouble(et1.getText() + "");
当您使用运算符解析整个字符串时,您必须使用

et1.getText().toString();

希望这有帮助。

在onCreate中初始化EditText et1、tv,然后在onclick中按此格式更改

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

et1似乎还没有初始化。谢谢你的帮助。我还想问一下如何为2+编码,然后按=以便系列2,4,6…应该继续