解析双如果是null崩溃Android应用程序

时间:2015-03-31 08:13:06

标签: android

我有以下代码。就像计算器。它检查3个文本字段是否正在更改,然后返回到另一个字符串结果。除非用户删除et4字符串上的值然后应用程序崩溃,否则一切正常。这是我的代码

package com.example.b15_calc;

import com.example.b15_calc.R;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    EditText et1, et2, et3, et4, et5;
    Button b1, b2;
    Button bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9, bt0, btdot, btbcsp, btnext;
    EditText etfocused;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et1 = (EditText) findViewById(R.id.editText1);
        et2 = (EditText) findViewById(R.id.editText2);
        et3 = (EditText) findViewById(R.id.editText3);
        et4 = (EditText) findViewById(R.id.editText4);
        et5 = (EditText) findViewById(R.id.editText5);

        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);

        bt1 = (Button) findViewById(R.id.num1);
        bt2 = (Button) findViewById(R.id.num2);
        bt3 = (Button) findViewById(R.id.num3);
        bt4 = (Button) findViewById(R.id.num4);
        bt5 = (Button) findViewById(R.id.num5);
        bt6 = (Button) findViewById(R.id.num6);
        bt7 = (Button) findViewById(R.id.num7);
        bt8 = (Button) findViewById(R.id.num8);
        bt9 = (Button) findViewById(R.id.num9);
        bt0 = (Button) findViewById(R.id.num0);
        btdot = (Button) findViewById(R.id.numdot);
        btbcsp = (Button) findViewById(R.id.backspace);
        btnext = (Button) findViewById(R.id.next);

        // call of method to hide default keyboard
        hideKeyboard();
        et1.setInputType(InputType.TYPE_NULL);
        et2.setInputType(InputType.TYPE_NULL);
        et4.setInputType(InputType.TYPE_NULL);

        // to check which edit text is focussed
        et1.setOnFocusChangeListener(new OnFocusChangeListener() {

            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // code to execute when EditText loses focus
                    etfocused = et1;
                }
            }
        });

        et2.setOnFocusChangeListener(new OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // code to execute when EditText loses focus
                    etfocused = et2;
                }
            }
        });

        et4.setOnFocusChangeListener(new OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // code to execute when EditText loses focus
                    etfocused = et4;
                }
            }
        });

        // coding for buttons
        bt0.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                etfocused.append("0");
            }
        });
        bt1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                etfocused.append("1");
            }
        });
        bt2.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                etfocused.append("2");
            }
        });
        bt3.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                etfocused.append("3");
            }
        });
        bt4.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                etfocused.append("4");
            }
        });
        bt5.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                etfocused.append("5");
            }
        });
        bt6.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                etfocused.append("6");
            }
        });
        bt7.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                etfocused.append("7");
            }
        });
        bt8.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                etfocused.append("8");
            }
        });
        bt9.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                etfocused.append("9");
            }
        });
        btdot.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                etfocused.append(".");
            }
        });
        btbcsp.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                String txt = etfocused.getText().toString();
                txt = txt.length() > 1 ? txt.substring(0, txt.length() - 1) : "";
                etfocused.setText(txt);
            }
        });
        btnext.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if (etfocused == et1) {
                    et2.requestFocus();
                } else if (etfocused == et2) {
                    et4.requestFocus();
                } else if (etfocused == et4) {
                    et1.requestFocus();
                }
            }
        });


        //step3 : write add functionality.
        b2.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                finish();
                startActivity(getIntent());
                et1.requestFocus();

            }
        });

        //step3 : ΜΕΤΑΒΛΗΤΕΣ
        et4.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence ss, int start, int before, int count) {

                // TODO Auto-generated method stub
            }

            @Override
            public void beforeTextChanged(CharSequence ss, int start, int count, int after) {

                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable ss) {


                // make the calculation here and set the result to et3

                String f = et1.getText().toString();
                double i = Double.parseDouble(f);
                String s = et2.getText().toString();
                double j = Double.parseDouble(s);
                String w = et4.getText().toString();


                double q = Double.parseDouble(w);

                double price_gold = 8662.11;
                double fpa = 1.23;
                double fpol = 0.10;
                double fpolam = 999;
                double isot = 273.014;
                double sint_ker = 2.6026;
                double result1 = (i * price_gold) + (j * 1000) + (q * isot);
                double result2 = result1 / 340.75;
                int gap;

                if (result2 >= fpolam) {
                    double result = (result2 * fpol);
                    double result3 = ((result2 * sint_ker) * fpa) + result;
                    if (result3 < 1000) {
                        gap = 10;
                    } else if (result3 < 5000) {
                        gap = 50;
                    } else //5000+
                    {
                        gap = 100;
                    }
                    int total = (int) Math.ceil(result3 / gap) * gap;


                    String res = String.valueOf(total);
                    et3.setText(res);

                } else {

                    double result3 = ((result2 * sint_ker) * fpa);
                    if (result3 < 1000) {
                        gap = 10;
                    } else if (result3 < 5000) {
                        gap = 50;
                    } else //5000+
                    {
                        gap = 100;
                    }

                    int total = (int) Math.ceil(result3 / gap) * gap;
                    String res = String.valueOf(total);
                    et3.setText(res);

                }
            }

        });


    }
// to hide default keyboard

    private void hideKeyboard() {
        // Check if no view has focus:
        View view = this.getCurrentFocus();
        if (view != null) {
            InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

2 个答案:

答案 0 :(得分:2)

向我们提供准确的崩溃日志真的很不错......否则我们就可以在黑暗中拍摄。

无论如何,您很可能会在NumberFormatException行中double i = Double.parseDouble(f);点击<{1}}

尝试使用try-catch这样的解析操作:

try {
    double i = Double.parseDouble(f);
    ......
catch (NumberFormatException e) {
    // Handle error here, perhaps notify the user to input some data        
}

答案 1 :(得分:0)

当您尝试解析空字符串NumberFormatException时,可能会引发''。如Veko的答案所示,您可以使用Double.parseDouble(f)附上try-catch语句,或者更恰当地说,检查字符串是否为空:

if (!TextUtils.isEmpty(f)) {
    double i = Double.parseDouble(f);
}

如果字符串为空则不执行任何操作。