2015-09-17 44 views
0

如果我嘗試啓動我的應用程序,它失敗並顯示此錯誤:應用失敗與java.lang.NumberFormatException:無效INT

09-17 20:41:18.190 2063-2063/cz.test.sudoman281.lesnicketabulky E/AndroidRuntime? FATAL EXCEPTION: main 
    Process: cz.test.sudoman281.lesnicketabulky, PID: 2063 
    java.lang.NumberFormatException: Invalid int: "" 
      at java.lang.Integer.invalidInt(Integer.java:138) 
      at java.lang.Integer.parseInt(Integer.java:358) 
      at java.lang.Integer.parseInt(Integer.java:334) 
      at cz.test.sudoman281.lesnicketabulky.MainActivity$1.onFocusChange(MainActivity.java:36) 
      at android.view.View.onFocusChanged(View.java:5192) 
      at android.widget.TextView.onFocusChanged(TextView.java:7989) 
      at android.view.View.handleFocusGainInternal(View.java:4948) 
      at android.view.View.requestFocusNoSearch(View.java:7547) 
      at android.view.View.requestFocus(View.java:7526) 
      at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2557) 
      at android.view.ViewGroup.requestFocus(ViewGroup.java:2513) 
      at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2557) 
      at android.view.ViewGroup.requestFocus(ViewGroup.java:2513) 
      at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2557) 
      at android.view.ViewGroup.requestFocus(ViewGroup.java:2513) 
      at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2557) 
      at android.view.ViewGroup.requestFocus(ViewGroup.java:2513) 
      at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2557) 
      at android.view.ViewGroup.requestFocus(ViewGroup.java:2513) 
      at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2557) 
      at android.view.ViewGroup.requestFocus(ViewGroup.java:2516) 
      at android.view.View.requestFocus(View.java:7493) 
      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1968) 
      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1087) 
      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6040) 
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:793) 
      at android.view.Choreographer.doCallbacks(Choreographer.java:606) 
      at android.view.Choreographer.doFrame(Choreographer.java:575) 
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:779) 
      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:5538) 
      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:960) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

我的代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Button spocitat = (Button) findViewById(R.id.spocitat); 
    EditText delka = (EditText) findViewById(R.id.delka); 
    EditText tloustka = (EditText) findViewById(R.id.tloustka); 

    delka.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      EditText delka = (EditText) findViewById(R.id.delka); 
      EditText tloustka = (EditText) findViewById(R.id.tloustka); 
      TextView prumer = (TextView) findViewById(R.id.prumer); 
      RadioButton smrk = (RadioButton) findViewById(R.id.smrk); 
      RadioButton borovice = (RadioButton) findViewById(R.id.borovice); 
      RadioButton boroveOddenky = (RadioButton) findViewById(R.id.oddenky); 
      RadioButton buk = (RadioButton) findViewById(R.id.buk); 
      RadioButton dub = (RadioButton) findViewById(R.id.dub); 

      if (delka != null && tloustka != null) { 
       int L = Integer.parseInt(delka.getText().toString()); 
       int Dsk = Integer.parseInt(tloustka.getText().toString()); 
       if (smrk.isChecked()) { 
        double p0 = Math.pow(5.7723 * 10, -1); 
        double p1 = Math.pow(6.8968 * 10, -3); 
        double p2 = Math.pow(1.3123 * 10, 0); 
        double Dp2 = Math.pow(Dsk, p2); 
        double pi = 3.14159; 
        double cast1 = Math.pow(Dsk - (p0 + p1 * Dp2), 2); 
        int vypocet = (int) Math.floor(((cast1 * pi * L)/40000) * 100); 
        prumer.setText(String.valueOf(vypocet)); 
       } else if (borovice.isChecked()) { 

       } else if (boroveOddenky.isChecked()) { 

       } else if (buk.isChecked()) { 

       } else if (dub.isChecked()) { 

       } else { 
        prumer.setText("Musíte zvolit drevinu!"); 
       } 
      } 
     } 
    }); 
} 
+0

您應該檢查「String」,因爲它在將其解析爲Integer之前爲空或空白,您還可以在下面定義一些編輯文本兩次; EditText delka =(EditText)findViewById(R.id.delka); EditText tloustka =(EditText)findViewById(R.id.tloustka); 你也應該修復它們。 –

回答

1

這是因爲您嘗試解析的字符串整數在以下兩行之一中爲空:

int L = Integer.parseInt(delka.getText().toString()); 
int Dsk = Integer.parseInt(tloustka.getText().toString()); 

if (delka != null && tloustka != null)不能解決它,因爲您只檢查文本框實例是否爲空。你需要驗證他們的內容是否也是空的。

+0

@ sudoman281更新了答案。 – Amila

相關問題