2015-05-14 84 views
1

我已經看到了一些類似於我的其他標題,但是我的不同之處在於,輸入的字符串被解析爲雙精度,只要所有的editText字段都被填充,它就可以工作沒問題,但是如果一個字段留空,它會返回到最後一個活動!如果我誠實的話,logcat中的錯誤是模糊的,我認爲它談論的事實是沒有意圖在下一個活動,我已經實現,但它仍然崩潰的按鈕,所以我不知道在哪裏我錯了。任何意見,將不勝感激,這裏是我的代碼和logcat的錯誤:當editText字段爲空時

package com.example.siuni.mymedicare; 

import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class weighinscreen extends ActionBarActivity { 

    private Button result; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.weighinscreen); 

     result = (Button) findViewById(R.id.button1); 

     Button btn = (Button) findViewById(R.id.button1); 
     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 


       EditText txtTempC = (EditText) findViewById(R.id.editText1); 
       String strC = txtTempC.getText().toString(); 
       double TempC = Double.parseDouble(strC);//Parses the string as double 

       EditText txtTempF = (EditText) findViewById(R.id.editText2); 
       String strF = txtTempF.getText().toString(); 
       double TempF = Double.parseDouble(strF););//Parses the string as double 


       EditText txtBPS = (EditText) findViewById(R.id.editText3); 
       String strBPS = txtBPS.getText().toString(); 
       double BPS = Double.parseDouble(strBPS););//Parses the string as double 


       EditText txtBPD = (EditText) findViewById(R.id.editText4); 
       String strBPD = txtBPD.getText().toString(); 
       double BPD = Double.parseDouble(strBPD););//Parses the string as double 


       EditText txtHeartR = (EditText) findViewById(R.id.editText5); 
       String strH = txtHeartR.getText().toString(); 
       double HeartR = Double.parseDouble(strH););//Parses the string as double 


       if (Double.compare(TempC, Double.NaN) == 0) {//Should check the double to see if it null and produce 

        Toast.makeText(getApplicationContext(), "Please enter your temperature", 
          Toast.LENGTH_SHORT).show(); 
        result.setEnabled(true); 

       } else if (Double.compare(TempF, Double.NaN) == 0){//Should check the double to see if it null and produce the toast 


        Toast.makeText(getApplicationContext(), "Please enter your temperature", 
          Toast.LENGTH_SHORT).show(); 
        result.setEnabled(true); 
       } else if (Double.compare(BPS, Double.NaN) == 0) {//Should check the double to see if it null and produce the toast 


        Toast.makeText(getApplicationContext(), "Please enter your blood pressure", 
          Toast.LENGTH_LONG).show(); 
        result.setEnabled(true); 

       } else if (Double.compare(BPD, Double.NaN) == 0) {//Should check the double to see if it null and produce the toast 


        Toast.makeText(getApplicationContext(), "Please enter your blood pressure", 
          Toast.LENGTH_LONG).show(); 
        result.setEnabled(true); 

       } else if (Double.compare(HeartR, Double.NaN) == 0{//Should check the double to see if it null and produce the toast 


        Toast.makeText(getApplicationContext(), "Please enter your heart rate", 
          Toast.LENGTH_LONG).show(); 
        result.setEnabled(true); 

       } else if (TempC <=36 && TempF <= 99 && BPS <= 100 && BPD <= 40 && HeartR <= 60) {//If the doubles are equal to the figures produces the toast 

        Toast.makeText(getApplicationContext(), "Please enter contact your GP", 
          Toast.LENGTH_LONG).show(); 
        startActivity(new Intent(weighinclass.this, register.class));//moves on the next actvity 
        result.setEnabled(false); 

       } 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_main_activity2, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     int id = item.getItemId(); 

     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

和錯誤:如果你的EditText提供空值

05-14 08:57:56.944 2392-2407/com.example.siuni.mymedicare W/EGL_emulation﹕ eglSurfaceAttrib not implemented 
05-14 08:57:56.944 2392-2407/com.example.siuni.mymedicare W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6cc21c0, error=EGL_SUCCESS 

回答

0

應用程序會崩潰。

您在的EditText中的onClick & 得到的字符串(有時空)試圖解析它。

它會在您的代碼throw a NumberFormatException &你是not using any Exception handling mechanism。所以顯然你的應用程序會崩潰。

提供一些檢查,像

if(!strH.equals("")){  
double HeartR = Double.parseDouble(strH);); 
} 
else{ 
Toast.makeText(getApplicationContext(), "Please enter your Heart Rate",Toast.LENGTH_LONG).show(); 
} 

注:所提供的日誌不與您的問題有關。正確的日誌會是這樣的

+0

我試着按照你的建議來實現這樣的檢查,沒有明顯的錯誤。這是在模擬器和聯繫7! –

+0

請嘗試獲得logcat輸出 –

+0

由於我植入了代碼,這是來自logcat的錯誤: java.lang.NumberFormatException:無效的雙倍:「」 –

相關問題