2015-03-31 21 views
0

我有以下代碼。就像計算器。它檢查3個文本字段是否正在更改,然後返回到另一個字符串的結果。一切工作正常,除非用戶刪除et4字符串然後應用程序崩潰的值。這裏是我的代碼解析double如果爲null崩潰android應用

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; 
    } 

} 
+0

發佈您的日誌貓 – 2015-03-31 08:18:59

回答

1

這將是非常好的給我們準確的碰撞日誌,否則我們可以在黑暗中拍攝。

不管怎麼說,最有可能你打一個NumberFormatException在你行double i = Double.parseDouble(f);

嘗試圍繞這些解析操作與try-catch這樣的:

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

請你能更具體嗎?問題在於雙Q – teok1979 2015-03-31 12:05:33

0

這可能提出了一個NumberFormatException當您嘗試解析一個空字符串,''。你可以用try-catch sorround的Double.parseDouble(f)聲明中Veko的答案顯示,或者,更appropiated方式,只是檢查字符串不爲空:

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

,什麼也不做,如果字符串爲空。