我的计算器应用有什么问题?

时间:2015-11-08 21:37:57

标签: java android

我正在尝试创建一个基本的计算器应用程序。我只是想让这个基本的应用程序工作,它也包含一个小数点按钮。以下是我在java中编写和收集的代码:

public class MainActivity extends Activity implements OnClickListener{

TextView main;
private long operand1;
private long operand2;
private long result;
private String operator;
private StringBuilder set1;
private StringBuilder set2;
Button zero;
Button one;
Button two;
Button three;
Button four;
Button five;
Button six;
Button seven;
Button eight;
Button nine;
Button times;
Button clear;
Button equal;
Button decimal;
Button divide;
Button add;
Button subtract;

/* (non-Javadoc)
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // declare all buttons used within the layout
    zero = (Button) findViewById(R.id.button0);
    one = (Button) findViewById(R.id.button1);
    two = (Button) findViewById(R.id.button2);
    three = (Button) findViewById(R.id.button3);
    four = (Button) findViewById(R.id.button4);
    five = (Button) findViewById(R.id.button5);
    six = (Button) findViewById(R.id.button6);
    seven = (Button) findViewById(R.id.button7);
    eight = (Button) findViewById(R.id.button8);
    nine = (Button) findViewById(R.id.button9);
    times = (Button) findViewById(R.id.buttontimes);
    clear = (Button) findViewById(R.id.buttonClear);
    equal = (Button) findViewById(R.id.buttonEqual);
    decimal = (Button) findViewById(R.id.buttonDecimal);
    divide = (Button) findViewById(R.id.buttondivide);
    add = (Button) findViewById(R.id.buttonplus);
    subtract = (Button) findViewById(R.id.buttonminus);

    // declare main text view
    TextView main = (TextView) findViewById(R.id.CalculatorText);

    zero.setOnClickListener(this);
    one.setOnClickListener(this);
    two.setOnClickListener(this);
    three.setOnClickListener(this);
    four.setOnClickListener(this);
    five.setOnClickListener(this);
    six.setOnClickListener(this);
    seven.setOnClickListener(this);
    eight.setOnClickListener(this);
    nine.setOnClickListener(this);
    equal.setOnClickListener(this);
    subtract.setOnClickListener(this);
    times.setOnClickListener(this);
    divide.setOnClickListener(this);
    add.setOnClickListener(this);
    clear.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.button0:
            main.append(zero.getText());
            if(operand1 == -1)
            {
                set1.append(zero.getText().toString());
            }
            else
            {
                set2.append(zero.getText().toString());
            }
            break;

        case R.id.button1:
            main.append(one.getText());
            if(operand1 == -1)
            {
                set1.append(one.getText().toString());
            }
            else
            {
                set2.append(one.getText().toString());
                operand2 = Long.parseLong(set2.toString());
            }
            break;

        case R.id.button2:
            main.append(two.getText());
            if(operand1 == -1)
            {
                set1.append(two.getText().toString());
            }
            else
            {
                set2.append(two.getText().toString());
                operand2 = Long.parseLong(set2.toString());
            }
            break;

        case R.id.button3:
            main.append(three.getText());
            if(operand1 == -1)
            {
                set1.append(three.getText().toString());
            }
            else
            {
                set2.append(three.getText().toString());
            }
            break;

        case R.id.button4:
            main.append(four.getText());
            if(operand1 == -1)
            {
                set1.append(four.getText().toString());
            }
            else
            {
                set2.append(four.getText().toString());
            }
            break;

        case R.id.button5:
            main.append(five.getText());
            if(operand1 == -1)
            {
                set1.append(five.getText().toString());
            }
            else
            {
                set2.append(five.getText().toString());
            }
            break;

        case R.id.button6:
            main.append(six.getText());
            if(operand1 == -1)
            {
                set1.append(six.getText().toString());
            }
            else
            {
                set2.append(six.getText().toString());
            }
            break;

        case R.id.button7:
            main.append(seven.getText());
            if(operand1 == -1)
            {
                set1.append(seven.getText().toString());
            }
            else
            {
                set2.append(seven.getText().toString());
            }
            break;

        case R.id.button8:
            main.append(eight.getText());
            if(operand1 == -1)
            {
                set1.append(eight.getText().toString());
            }
            else
            {
                set2.append(eight.getText().toString());
            }
            break;

        case R.id.button9:
            main.append(nine.getText());
            if(operand1 == -1)
            {
                set1.append(nine.getText().toString());
            }
            else
            {
                set2.append(nine.getText().toString());
            }
            break;

        case R.id.buttonplus:
            if(operand1 == -1)
            {
                operator = "+";
                operand1 = Long.parseLong(set1.toString());
                main.append(operator);
            }
            break;

        case R.id.buttonminus:
            if(operand1 == -1)
            {
                operator = "-";
                operand1 = Long.parseLong(set1.toString());
                main.append(operator);
            }
            break;

        case R.id.buttontimes:
            if(operand1 == -1)
            {
                operator = "*";
                operand1 = Long.parseLong(set1.toString());
                main.append(operator);
            }
            break;

        case R.id.buttondivide:
            if(operand1 == -1)
            {
                operator = "/";
                operand1 = Long.parseLong(set1.toString());
                main.append(operator);
            }
            break;

        case R.id.buttonEqual:
            operand2 = Long.parseLong(set2.toString());
            switch(operator)
            {
                case "+":
                    result = operand1 + operand2;

                case "-":
                    result = operand1 - operand2;

                case "*":
                    result = operand1 * operand2;

                case "/":
                    result = operand1 / operand2;
            }
            main.setText(""+result);
            operator = null;
            operand1 = -1;
            operand2 = -1;
            set1.setLength(0);
            set2.setLength(0);
            break;

        case R.id.buttonClear:
            main.setText("");
            operator = null;
            operand1 = -1;
            operand2 = -1;
            set1.setLength(0);
            set2.setLength(0);
            break;
        case R.id.buttonDecimal:
            main.append(decimal.getText());
            if(operand1 == -1)
            {
                set1.append(decimal.getText().toString());
            }
            else
            {
                set2.append(decimal.getText().toString());
            }
            break;
    }
}

我的错误信息是:

11-08 15:14:45.521  26350-26350/com.example.chrisjw1.lab2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.chrisjw1.lab2, PID: 26350
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.append(java.lang.CharSequence)' on a null object reference
        at com.example.lab2.MainActivity.onClick(MainActivity.java:138)
        at android.view.View.performClick(View.java:4764)
        at android.view.View$PerformClick.run(View.java:19833)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5290)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

它说我从getText调用返回的数据为null。任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

TextView main 在外部声明为私有对象,因此方法onClick()看到在TextView之外声明的“first”onCreate()和它没有看到内部TextView 所以替换这个:

TextView main = (TextView) findViewById(R.id.CalculatorText);

使用:

main = (TextView) findViewById(R.id.CalculatorText);

全班:

public class MainActivity extends Activity implements OnClickListener{

TextView main;
private long operand1;
private long operand2;
private long result;
private String operator;
private StringBuilder set1;
private StringBuilder set2;
Button zero;
Button one;
Button two;
Button three;
Button four;
Button five;
Button six;
Button seven;
Button eight;
Button nine;
Button times;
Button clear;
Button equal;
Button decimal;
Button divide;
Button add;
Button subtract;

/* (non-Javadoc)
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // declare all buttons used within the layout
    zero = (Button) findViewById(R.id.button0);
    one = (Button) findViewById(R.id.button1);
    two = (Button) findViewById(R.id.button2);
    three = (Button) findViewById(R.id.button3);
    four = (Button) findViewById(R.id.button4);
    five = (Button) findViewById(R.id.button5);
    six = (Button) findViewById(R.id.button6);
    seven = (Button) findViewById(R.id.button7);
    eight = (Button) findViewById(R.id.button8);
    nine = (Button) findViewById(R.id.button9);
    times = (Button) findViewById(R.id.buttontimes);
    clear = (Button) findViewById(R.id.buttonClear);
    equal = (Button) findViewById(R.id.buttonEqual);
    decimal = (Button) findViewById(R.id.buttonDecimal);
    divide = (Button) findViewById(R.id.buttondivide);
    add = (Button) findViewById(R.id.buttonplus);
    subtract = (Button) findViewById(R.id.buttonminus);

    //Use outside textview delcared
    main = (TextView) findViewById(R.id.CalculatorText);

    zero.setOnClickListener(this);
    one.setOnClickListener(this);
    two.setOnClickListener(this);
    three.setOnClickListener(this);
    four.setOnClickListener(this);
    five.setOnClickListener(this);
    six.setOnClickListener(this);
    seven.setOnClickListener(this);
    eight.setOnClickListener(this);
    nine.setOnClickListener(this);
    equal.setOnClickListener(this);
    subtract.setOnClickListener(this);
    times.setOnClickListener(this);
    divide.setOnClickListener(this);
    add.setOnClickListener(this);
    clear.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.button0:
            main.append(zero.getText());
            if(operand1 == -1)
            {
                set1.append(zero.getText().toString());
            }
            else
            {
                set2.append(zero.getText().toString());
            }
            break;

        case R.id.button1:
            main.append(one.getText());
            if(operand1 == -1)
            {
                set1.append(one.getText().toString());
            }
            else
            {
                set2.append(one.getText().toString());
                operand2 = Long.parseLong(set2.toString());
            }
            break;

        case R.id.button2:
            main.append(two.getText());
            if(operand1 == -1)
            {
                set1.append(two.getText().toString());
            }
            else
            {
                set2.append(two.getText().toString());
                operand2 = Long.parseLong(set2.toString());
            }
            break;

        case R.id.button3:
            main.append(three.getText());
            if(operand1 == -1)
            {
                set1.append(three.getText().toString());
            }
            else
            {
                set2.append(three.getText().toString());
            }
            break;

        case R.id.button4:
            main.append(four.getText());
            if(operand1 == -1)
            {
                set1.append(four.getText().toString());
            }
            else
            {
                set2.append(four.getText().toString());
            }
            break;

        case R.id.button5:
            main.append(five.getText());
            if(operand1 == -1)
            {
                set1.append(five.getText().toString());
            }
            else
            {
                set2.append(five.getText().toString());
            }
            break;

        case R.id.button6:
            main.append(six.getText());
            if(operand1 == -1)
            {
                set1.append(six.getText().toString());
            }
            else
            {
                set2.append(six.getText().toString());
            }
            break;

        case R.id.button7:
            main.append(seven.getText());
            if(operand1 == -1)
            {
                set1.append(seven.getText().toString());
            }
            else
            {
                set2.append(seven.getText().toString());
            }
            break;

        case R.id.button8:
            main.append(eight.getText());
            if(operand1 == -1)
            {
                set1.append(eight.getText().toString());
            }
            else
            {
                set2.append(eight.getText().toString());
            }
            break;

        case R.id.button9:
            main.append(nine.getText());
            if(operand1 == -1)
            {
                set1.append(nine.getText().toString());
            }
            else
            {
                set2.append(nine.getText().toString());
            }
            break;

        case R.id.buttonplus:
            if(operand1 == -1)
            {
                operator = "+";
                operand1 = Long.parseLong(set1.toString());
                main.append(operator);
            }
            break;

        case R.id.buttonminus:
            if(operand1 == -1)
            {
                operator = "-";
                operand1 = Long.parseLong(set1.toString());
                main.append(operator);
            }
            break;

        case R.id.buttontimes:
            if(operand1 == -1)
            {
                operator = "*";
                operand1 = Long.parseLong(set1.toString());
                main.append(operator);
            }
            break;

        case R.id.buttondivide:
            if(operand1 == -1)
            {
                operator = "/";
                operand1 = Long.parseLong(set1.toString());
                main.append(operator);
            }
            break;

        case R.id.buttonEqual:
            operand2 = Long.parseLong(set2.toString());
            switch(operator)
            {
                case "+":
                    result = operand1 + operand2;

                case "-":
                    result = operand1 - operand2;

                case "*":
                    result = operand1 * operand2;

                case "/":
                    result = operand1 / operand2;
            }
            main.setText(""+result);
            operator = null;
            operand1 = -1;
            operand2 = -1;
            set1.setLength(0);
            set2.setLength(0);
            break;

        case R.id.buttonClear:
            main.setText("");
            operator = null;
            operand1 = -1;
            operand2 = -1;
            set1.setLength(0);
            set2.setLength(0);
            break;
        case R.id.buttonDecimal:
            main.append(decimal.getText());
            if(operand1 == -1)
            {
                set1.append(decimal.getText().toString());
            }
            else
            {
                set2.append(decimal.getText().toString());
            }
            break;
    }
}