2013-11-24 28 views
0

我添加了一個簡單的if語句來檢查用戶輸入的值是否大於零,而我將editText輸入設置爲一個變量。添加輸入驗證後,變量無法解析

但添加的if語句下面的變量後,我得到這個錯誤:getoffsetlength cannot be resolved to a variable

我這是怎麼實現的輸入驗證below.Can有人建議我爲我在做什麼不對的實施?我在想的是變量getoffsetLength etc ..已經超出範圍後添加if子句。但我該如何解決這個問題?

if (offsetLength.getText().length() > 0) { 
      String getoffsetlength = offsetLength.getText().toString(); 
      } 

      if (offsetDepth.getText().length() > 0) { 
      String getoffsetdepth = offsetDepth.getText().toString(); 
      } 
      if (ductDepth.getText().length() > 0) { 
      String getductdepth = ductDepth.getText().toString(); 
      } 
      double tri1,tri2; 
      //double marking1,marking2,marking3; 

      //error here on all three variables 
      double off1 = Double.parseDouble(getoffsetlength); 
      double off2 = Double.parseDouble(getoffsetdepth); 
      double off3 = Double.parseDouble(getductdepth); 
+0

聲明爲類成員 – Raghunandan

+0

你能進一步解釋嗎? –

+0

在'onCreate'之前聲明'String getoffsetlength'作爲類成員 – Raghunandan

回答

0

嘗試初始化變量getoffsetlength,getoffsetdepth,getductdepth

String getoffsetlength,getoffsetdepth,getductdepth = null; 
if (offsetLength.getText().length() > 0) { 
    getoffsetlength = offsetLength.getText().toString(); 
} 

if (offsetDepth.getText().length() > 0) { 
    getoffsetdepth = offsetDepth.getText().toString(); 
} 
if (ductDepth.getText().length() > 0) { 
    getductdepth = ductDepth.getText().toString(); 
} 
double tri1,tri2; 
//double marking1,marking2,marking3; 

// validate whether the variables are not null 
if(getoffsetlength != null){ 
    double off1 = Double.parseDouble(getoffsetlength); 
} 
if(getoffsetdepth != null){ 
    double off2 = Double.parseDouble(getoffsetdepth); 
} 
if(getductdepth != null){ 
double off3 = Double.parseDouble(getductdepth); 
} 
+0

如果輸入的是空值,這會足以防止應用程序崩潰嗎?墜毀。 –