按下任何操作员按钮时Android计算器应用程序停止

时间:2018-02-12 06:55:38

标签: android calculator

我使用以下代码开发了一个简单的计算器应用程序:

document = new XmlDocument();
XmlNode docNode = document.CreateXmlDeclaration("1.0", "UTF-8", null);
document.CreateAttribute("EncryptionKey");
document.CreateAttribute("SaltKey");
document.CreateAttribute("VIKey");
document.CreateAttribute("EmailUsername");
document.CreateAttribute("EmailPassword");
document.AppendChild(docNode);
document.Save("Credentials.xml");

按数字按钮工作正常,但每当我按下任何操作按钮(如加,减,乘,除)时,应用程序就会停止。 我无法知道问题所在。请帮忙!

logcat in' Debug'模式有以下信息:

package com.example.sparsh.newcalculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView textView1;
    Button button0;
    Button button1;
    Button button2;
    Button button3;
    Button button4;
    Button button5;
    Button button6;
    Button button7;
    Button button8;
    Button button9;
    ImageButton buttonPlus;
    ImageButton buttonMinus;
    ImageButton buttonMultiply;
    ImageButton buttonDivide;
    String a, b;
    int result, result2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView1 = (TextView)findViewById(R.id.textView1);
        button0 = (Button)findViewById(R.id.button0);
        button1 = (Button)findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);
        button3 = (Button)findViewById(R.id.button3);
        button4 = (Button)findViewById(R.id.button4);
        button5 = (Button)findViewById(R.id.button5);
        button6 = (Button)findViewById(R.id.button6);
        button7 = (Button)findViewById(R.id.button7);
        button8 = (Button)findViewById(R.id.button8);
        button9 = (Button)findViewById(R.id.button9);
        buttonPlus = (ImageButton)findViewById(R.id.buttonPlus);
        buttonMinus = (ImageButton)findViewById(R.id.buttonMinus);
        buttonMultiply= (ImageButton)findViewById(R.id.buttonMultiply);
        buttonDivide = (ImageButton)findViewById(R.id.buttonDivide);
        a = " ";
        b = " ";
        result=0;
        result2=1;

        button0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a = a+button0.getText().toString();
                textView1.setText(a);
            }
        });

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            a = a + button1.getText().toString();
            textView1.setText(a);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a = a + button2.getText().toString();
                textView1.setText(a);
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a = a + button3.getText().toString();
                textView1.setText(a);
            }
        });

        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a = a + button4.getText().toString();
                textView1.setText(a);
            }
        });

        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a = a + button5.getText().toString();
                textView1.setText(a);
            }
        });

        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a = a + button6.getText().toString();
                textView1.setText(a);            
            }
        });

        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a = a + button7.getText().toString();
                textView1.setText(a);
            }
        });

        button8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a = a + button8.getText().toString();
                textView1.setText(a);
            }
        });

        button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a = a + button9.getText().toString();
                textView1.setText(a);
            }
        });

        buttonPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //textView1.setText(" ");
                result  =   Integer.parseInt(a) + result;

                textView1.setText(Integer.toString(result));
            }
        });

        buttonMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //  textView1.setText(" ");

                result = Integer.parseInt(a) - result;
                textView1.setText(Integer.toString(result));
            }
        });

        buttonMultiply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //textView1.setText(" ");

                result2 = Integer.parseInt(a) * result2;
                textView1.setText(Integer.toString(result2));
            }
        });

        buttonDivide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //textView1.setText(" ");

                result2 = Integer.parseInt(a) / result2;
                textView1.setText(Integer.toString(result2));
            }
        });
    }   
}

我没有实现“等于”#39;按钮现在。但按下操作按钮应显示结果。

2 个答案:

答案 0 :(得分:1)

失败的原因是因为你的堆栈跟踪" "之前的666空间。您需要确保您的String变量a是一个空字符串,即""而不是具有空格的字符串,即" "。尝试使用a.trim()删除空格

答案 1 :(得分:1)

你正试图解析" 666"其中包含一个空格,从而产生NumberFormatException。您必须确保要解析的字符串没有任何不是数字的字符。使用String.trim()删除任何前导/尾随空格,但在尝试解析值之前,还要确保使用String.isEmpty()解析的值不是空字符串。