2013-04-25 194 views
0

我創建了一個將美元兌換成歐元的應用程序,要求在沒有輸入的情況下,不會顯示結果。我這樣的代碼:返回空結果?

case R.id.main_btn_convert: 

     String input = mTvBaseCurrency.getText().toString(); 


     if (input.matches("")) { 
      Toast.makeText(this, "Type the number!", Toast.LENGTH_SHORT).show(); 
      mTvBaseCurrency = null; 

      Log.i("String", ",asdkns,adkjsahd "); 
      break; 
     } 

     else { 


      String url = currencyUtils.createUrl(strBase, strTarget, input); 
      Log.i("String", "url= " + url); 

      currencyUtils.handlerJson(url); 

      String result = currencyUtils.getStringResult(); 
      Log.i("String ", "result" + result); 

      mTvTargetCurrency.setText("" + result); 
      break; 
     } 

問題開始apear,在第一個點擊(沒有輸入數據),就OK了,但第二次時,單擊它有錯誤:強制關閉應用程序。這是我的logcat http://s17.postimg.org/6uaprjm5b/Untitled.png 請幫我解決這個問題。

+0

「MainPageActivity」的第200行是什麼?你不需要'if/else'中的'break' – codeMagic 2013-04-25 02:29:09

+0

String input = mTvBaseCurrency.getText()。toString(); – 2013-04-25 02:30:36

+0

這是獲取我輸入的字符串:) – 2013-04-25 02:31:11

回答

2

問題是如果它通過matches(""),您將mTvBaseCurrency設置爲null。只需刪除以下行:

mTvBaseCurrency = null; 
+0

mTvBaseCurrency = null;當沒有輸入時返回空結果:(。 – 2013-04-25 02:45:31

+0

@ItDn我看到你會返回一個沒有它的空結果。如果你進入if子句,你不會進入else子句,否則,你總是可以創建一個布爾值或其他東西來檢查是否有結果。 – ddmps 2013-04-25 02:52:37