Android基本计算器

时间:2014-08-29 19:51:50

标签: android calculator

我正在尝试构建一个基本的计算器。代码很好(我猜)。我在运行时没有收到任何错误,eclipse成功编译它。几乎一切都很好但是当我在editText视图中输入数字然后尝试通过点击"计算"来做一些计算。按钮应用程序崩溃。这是崩溃应用程序的代码:

    private OnClickListener calcListener = new OnClickListener() {

        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub
            String f = first.getText().toString();
            String s = second.getText().toString();
            float fValue = Float.valueOf(f);
            float sValue = Float.valueOf(s);
            if((f.length() == 0) || (s.length() == 0))
                Toast.makeText(MainActivity.this, "Please enter data in the first section", Toast.LENGTH_LONG).show();
            else{   
                switch(sign.getCheckedRadioButtonId())
                {
                case R.id.mult:
                    float res_Mult = fValue*sValue;
                    result.setText(String.valueOf(res_Mult));
                case R.id.plus:
                    float res_Add = fValue*sValue;
                    result.setText(String.valueOf(res_Add));
                case R.id.minus:
                    float res_Minus = fValue*sValue;
                    result.setText(String.valueOf(res_Minus));
                case R.id.div :
                    float res_Div = fValue*sValue;
                    result.setText(String.valueOf(res_Div));
                }// end switch
            }// end else
        }
    };// end onclik

    Listener

问题似乎是计算,因为我试图输出"你好"在textview上,一旦按钮"计算"该文本视图应该显示计算结果。点击它就可以了。

Button calc = null;
    EditText first = null;
    EditText second = null;
    RadioGroup sign = null;
    TextView result = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //binding 
        calc = (Button) findViewById(R.id.calc);// the "calculate" button
        first = (EditText) findViewById(R.id.first); //the first EditText
        second = (EditText) findViewById(R.id.second);//the second EditText
        result = (TextView) findViewById(R.id.result);// The textView that this supposed to display the result.

        //setting listeners
        calc.setOnClickListener(calcListener);
        first.addTextChangedListener(watcher);
        second.addTextChangedListener(watcher);
    }

    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            result.setText(" ");
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    };// end textWatcher

这是logCat:

    08-29 19:18:29.323: D/dalvikvm(1951): Not late-enabling CheckJNI (already on)
    08-29 19:18:31.593: D/dalvikvm(1951): GC_FOR_ALLOC freed 72K, 10% free 2892K/3192K, paused 108ms, total 111ms
    08-29 19:18:32.183: D/libEGL(1951): loaded /system/lib/egl/libEGL_emulation.so
    08-29 19:18:32.213: D/(1951): HostConnection::get() New Host Connection established 0xb8cfd430, tid 1951
    08-29 19:18:32.223: D/libEGL(1951): loaded /system/lib/egl/libGLESv1_CM_emulation.so
    08-29 19:18:32.233: D/libEGL(1951): loaded /system/lib/egl/libGLESv2_emulation.so
    08-29 19:18:32.313: W/EGL_emulation(1951): eglSurfaceAttrib not implemented
    08-29 19:18:32.323: D/OpenGLRenderer(1951): Enabling debug mode 0
    08-29 19:18:50.674: D/AndroidRuntime(1951): Shutting down VM
    08-29 19:18:50.674: W/dalvikvm(1951): threadid=1: thread exiting with uncaught exception (group=0xb0eb8648)
    08-29 19:18:50.724: E/AndroidRuntime(1951): FATAL EXCEPTION: main
    08-29 19:18:50.724: E/AndroidRuntime(1951): java.lang.NullPointerException
    08-29 19:18:50.724: E/AndroidRuntime(1951):     at com.example.gets.calculator.MainActivity$2.onClick(MainActivity.java:76)
    08-29 19:18:50.724: E/AndroidRuntime(1951):     at android.view.View.performClick(View.java:4240)
    08-29 19:18:50.724: E/AndroidRuntime(1951):     at android.view.View$PerformClick.run(View.java:17721)
    08-29 19:18:50.724: E/AndroidRuntime(1951):     at android.os.Handler.handleCallback(Handler.java:730)
    08-29 19:18:50.724: E/AndroidRuntime(1951):     at android.os.Handler.dispatchMessage(Handler.java:92)
    08-29 19:18:50.724: E/AndroidRuntime(1951):     at android.os.Looper.loop(Looper.java:137)
    08-29 19:18:50.724: E/AndroidRuntime(1951):     at android.app.ActivityThread.main(ActivityThread.java:5103)
    08-29 19:18:50.724: E/AndroidRuntime(1951):     at java.lang.reflect.Method.invokeNative(Native Method)
    08-29 19:18:50.724: E/AndroidRuntime(1951):     at java.lang.reflect.Method.invoke(Method.java:525)
    08-29 19:18:50.724: E/AndroidRuntime(1951):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    08-29 19:18:50.724: E/AndroidRuntime(1951):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    08-29 19:18:50.724: E/AndroidRuntime(1951):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您的switch语句没有中断。这是一个错误,它会导致每个案例都落到下一个案例中,并在其下面的所有案例中执行每一段代码。

    switch(sign.getCheckedRadioButtonId()) {
      case R.id.mult:
        float res_Mult = fValue*sValue;
        result.setText(String.valueOf(res_Mult));
      case R.id.plus:
        float res_Add = fValue*sValue;
        result.setText(String.valueOf(res_Add));
      case R.id.minus:
        float res_Minus = fValue*sValue;
        result.setText(String.valueOf(res_Minus));
      case R.id.div :
        float res_Div = fValue*sValue;
        result.setText(String.valueOf(res_Div));
    }// end switch