使用android的简单计算器不工作

时间:2012-08-27 18:32:07

标签: android android-layout

我是android新手。我使用android开发了一个简单的计算器但无法运行它。当我运行它时,我得到一个空指针异常。 activity_main.xml文件是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/txtCalc"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="0"
        />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:layout_weight="1"
         >

        <Button
            android:id="@+id/btnSeven"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="7" />

        <Button
            android:id="@+id/btnEight"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="8"/>

        <Button
            android:id="@+id/btnNine"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="9"/>

        <Button
            android:id="@+id/btnDivide"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="/"/>

        <Button
            android:id="@+id/btnC"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="C"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" 
        android:layout_weight="1"
        >

        <Button
            android:id="@+id/btnFour"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="4"
            />

        <Button
            android:id="@+id/btnFive"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="5"
            />

        <Button
            android:id="@+id/btnSix"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="6"
            />

        <Button
            android:id="@+id/btnMultiply"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="*"
            />

        <Button
            android:id="@+id/btnPerc"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="%"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnOne"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="1"
            />

        <Button
            android:id="@+id/btnTwo"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="2"
            />

        <Button
            android:id="@+id/btnThree"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="3"
            />

        <Button
            android:id="@+id/btnMinus"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="-"
            />

        <Button
            android:id="@+id/btnSqrRoot"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="√"
             />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnZero"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="0"
            />

        <Button
            android:id="@+id/btnDecimal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="."
            />

        <Button
            android:id="@+id/btnPM"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="+/-"
            />

        <Button
            android:id="@+id/btnPlus"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="+"
            />

        <Button
            android:id="@+id/btnEquals"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="="
            />
    </LinearLayout>

</LinearLayout>

和MainActivity文件的内容是

    package com.example.calculator;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnKeyListener;
    import android.widget.Button;
    import android.widget.EditText;

public class MainActivity extends Activity {

    private final String SDK_VERSION = "1";
    private final int MENUITEM_CLOSE = 300;

    /*
     * Edit Text and Button object initialization for simple calculator design.
     */
    private EditText txtCalc = null;
    private Button btnZero = null;
    private Button btnOne = null;
    private Button btnTwo = null;
    private Button btnThree = null;
    private Button btnFour = null;
    private Button btnFive = null;
    private Button btnSix = null;
    private Button btnSeven = null;
    private Button btnEight = null;
    private Button btnNine = null;
    private Button btnPlus = null;
    private Button btnMinus = null;
    private Button btnMultiply = null;
    private Button btnDivide = null;
    private Button btnEquals = null;
    private Button btnC = null;
    private Button btnDecimal = null;
    private Button btnBS = null;
    private Button btnPerc = null;
    private Button btnSqrRoot = null;
    private Button btnPM = null;

    private double num = 0;
    private int operator = 1;
    // 0 = nothing, 1 = plus, 2 = minus, 3 =
    // multiply, 4 = divide
    private boolean readyToClear = false;
    private boolean hasChanged = false;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(android.R.style.Theme_Black);
        setContentView(R.layout.activity_main);

        this.setTitle("SimpleCalculator " + SDK_VERSION);

        initControls();
        //initScreenLayout();
        reset();
    }
    private void initControls() {
        txtCalc = (EditText) findViewById(R.id.txtCalc);
        btnZero = (Button) findViewById(R.id.btnZero);
        btnOne = (Button) findViewById(R.id.btnOne);
        btnTwo = (Button) findViewById(R.id.btnTwo);
        btnThree = (Button) findViewById(R.id.btnThree);
        btnFour = (Button) findViewById(R.id.btnFour);
        btnFive = (Button) findViewById(R.id.btnFive);
        btnSix = (Button) findViewById(R.id.btnSix);
        btnSeven = (Button) findViewById(R.id.btnSeven);
        btnEight = (Button) findViewById(R.id.btnEight);
        btnNine = (Button) findViewById(R.id.btnNine);
        btnPlus = (Button) findViewById(R.id.btnPlus);
        btnMinus = (Button) findViewById(R.id.btnMinus);
        btnMultiply = (Button) findViewById(R.id.btnMultiply);
        btnDivide = (Button) findViewById(R.id.btnDivide);
        btnEquals = (Button) findViewById(R.id.btnEquals);
        btnC = (Button) findViewById(R.id.btnC);
        btnDecimal = (Button) findViewById(R.id.btnDecimal);
        btnPerc = (Button) findViewById(R.id.btnPerc);
        btnSqrRoot = (Button) findViewById(R.id.btnSqrRoot);
        btnPM = (Button) findViewById(R.id.btnPM);

        btnZero.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                handleNumber(0);
            }
        });
        btnOne.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                handleNumber(1);
            }
        });
        btnTwo.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                handleNumber(2);
            }
        });
        btnThree.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                handleNumber(3);
            }
        });
        btnFour.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                handleNumber(4);
            }
        });
        btnFive.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                handleNumber(5);
            }
        });
        btnSix.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                handleNumber(6);
            }
        });
        btnSeven.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                handleNumber(7);
            }
        });
        btnEight.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                handleNumber(8);
            }
        });
        btnNine.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                handleNumber(9);
            }
        });
        btnPlus.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                handleEquals(1);
            }
        });
        btnMinus.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                handleEquals(2);
            }
        });
        btnMultiply.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                handleEquals(3);
            }
        });
        btnDivide.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                handleEquals(4);
            }
        });
        btnEquals.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                handleEquals(0);
            }
        });
        btnC.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                reset();
            }
        });
        btnDecimal.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                handleDecimal();
            }
        });
        btnPM.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                handlePlusMinus();
            }
        });
        btnBS.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                handleBackspace();
            }
        });
        btnSqrRoot.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setValue(Double.toString(Math.sqrt(Double.parseDouble(txtCalc
                        .getText().toString()))));
            }
        });
        btnPerc.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setValue(Double.toString(num
                        * (0.01 * Double.parseDouble(txtCalc.getText()
                                .toString()))));
            }
        });

        txtCalc.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int i, android.view.KeyEvent e) {
                if (e.getAction() == KeyEvent.ACTION_DOWN) {
                    int keyCode = e.getKeyCode();

                    // txtCalc.append("["+Integer.toString(keyCode)+"]");

                    switch (keyCode) {
                    case KeyEvent.KEYCODE_0:
                        handleNumber(0);
                        break;

                    case KeyEvent.KEYCODE_1:
                        handleNumber(1);
                        break;

                    case KeyEvent.KEYCODE_2:
                        handleNumber(2);
                        break;

                    case KeyEvent.KEYCODE_3:
                        handleNumber(3);
                        break;

                    case KeyEvent.KEYCODE_4:
                        handleNumber(4);
                        break;

                    case KeyEvent.KEYCODE_5:
                        handleNumber(5);
                        break;

                    case KeyEvent.KEYCODE_6:
                        handleNumber(6);
                        break;

                    case KeyEvent.KEYCODE_7:
                        handleNumber(7);
                        break;

                    case KeyEvent.KEYCODE_8:
                        handleNumber(8);
                        break;

                    case KeyEvent.KEYCODE_9:
                        handleNumber(9);
                        break;

                    case 43:
                        handleEquals(1);
                        break;

                    case KeyEvent.KEYCODE_EQUALS:
                        handleEquals(0);
                        break;

                    case KeyEvent.KEYCODE_MINUS:
                        handleEquals(2);
                        break;

                    case KeyEvent.KEYCODE_PERIOD:
                        handleDecimal();
                        break;

                    case KeyEvent.KEYCODE_C:
                        reset();
                        break;

                    case KeyEvent.KEYCODE_SLASH:
                        handleEquals(4);
                        break;

                    case KeyEvent.KEYCODE_DPAD_DOWN:
                        return false;
                    }
                }

                return true;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 1, MENUITEM_CLOSE, "Close");

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENUITEM_CLOSE:
            finish();
            break;
        }

        return super.onOptionsItemSelected(item);
    }

    private void handleEquals(int newOperator) {
        if (hasChanged) {
            switch (operator) {
            case 1:
                num = num + Double.parseDouble(txtCalc.getText().toString());
                break;
            case 2:
                num = num - Double.parseDouble(txtCalc.getText().toString());
                break;
            case 3:
                num = num * Double.parseDouble(txtCalc.getText().toString());
                break;
            case 4:
                num = num / Double.parseDouble(txtCalc.getText().toString());
                break;
            }

            String txt = Double.toString(num);
            txtCalc.setText(txt);
            txtCalc.setSelection(txt.length());

            readyToClear = true;
            hasChanged = false;
        }

        operator = newOperator;
    }

    private void handleNumber(int num) {
        if (operator == 0)
            reset();

        String txt = txtCalc.getText().toString();
        if (readyToClear) {
            txt = "";
            readyToClear = false;
        } else if (txt.equals("0"))
            txt = "";

        txt = txt + Integer.toString(num);

        txtCalc.setText(txt);
        txtCalc.setSelection(txt.length());

        hasChanged = true;
    }

    private void setValue(String value) {
        if (operator == 0)
            reset();

        if (readyToClear) {
            readyToClear = false;
        }

        txtCalc.setText(value);
        txtCalc.setSelection(value.length());

        hasChanged = true;
    }

    private void handleDecimal() {
        if (operator == 0)
            reset();

        if (readyToClear) {
            txtCalc.setText("0.");
            txtCalc.setSelection(2);
            readyToClear = false;
            hasChanged = true;
        } else {
            String txt = txtCalc.getText().toString();

            if (!txt.contains(".")) {
                txtCalc.append(".");
                hasChanged = true;
            }
        }
    }

    private void handleBackspace() {
        if (!readyToClear) {
            String txt = txtCalc.getText().toString();
            if (txt.length() > 0) {
                txt = txt.substring(0, txt.length() - 1);
                if (txt.equals(""))
                    txt = "0";

                txtCalc.setText(txt);
                txtCalc.setSelection(txt.length());
            }
        }
    }

    private void handlePlusMinus() {
        if (!readyToClear) {
            String txt = txtCalc.getText().toString();
            if (!txt.equals("0")) {
                if (txt.charAt(0) == '-')
                    txt = txt.substring(1, txt.length());
                else
                    txt = "-" + txt;

                txtCalc.setText(txt);
                txtCalc.setSelection(txt.length());
            }
        }
    }

    private void reset() {
        num = 0;
        txtCalc.setText("0");
        txtCalc.setSelection(1);
        operator = 1;
    }
}

任何人都可以告诉我哪里出错了,因为无法弄清楚我的错误。

1 个答案:

答案 0 :(得分:0)

您声明了Button btnBS,但在调用setOnClickListener方法之前,您从未初始化它。