2017-06-10 85 views
0

我正在嘗試構建一個年齡計算器應用程序。當我沒有在EditTexts之一中輸入數字並點擊計算Button時,我的應用程序崩潰了! "App has stopped" and click the photo在計算數字時檢查EditText是否爲空

我試圖設置yearbirth.isEmpty() || yearbirth.equals("") || yearbirth == null,但沒有任何工作。

CODE: -

class MainActivity : AppCompatActivity() { 
var yearage:Int?= null 
var monthage:Int?= null 
var dayage:Int?= null 

@SuppressLint("SetTextI18n") 
override fun onCreate(savedInstanceState: Bundle?) { 
    super.onCreate(savedInstanceState) 
    setContentView(R.layout.activity_main) 

    buGetAge.setOnClickListener({ 
     val year= Calendar.getInstance().get(Calendar.YEAR) 
     val month= Calendar.getInstance().get(Calendar.MONTH) 
     val day= Calendar.getInstance().get(Calendar.DAY_OF_MONTH) 

     var yearage = year - Integer.parseInt(yearbirth.text.toString()) -1 
     val monthage:Int? 
     val dayage:Int? 

     if (yearbirth== null) { 
      Toast.makeText(this, "You have to enter your year berth", Toast.LENGTH_LONG).show() 
     } else { 
      if (month < Integer.parseInt(monthbirth.text.toString())){ 
       monthage=(Integer.parseInt(monthbirth.text.toString())- month- 12)* -1 
      }else if (month > Integer.parseInt(monthbirth.text.toString())) { 
       yearage=year- Integer.parseInt(yearbirth.text.toString()) 
       monthage= (Integer.parseInt(monthbirth.text.toString())- month)* -1 
      }else { 
       yearage=year- Integer.parseInt(yearbirth.text.toString()) 
       monthage=0 
      } 
      if (day < Integer.parseInt(daybirth.text.toString())){ 
       dayage= (Integer.parseInt(daybirth.text.toString())- day- 30)* -1 
      }else if (day > Integer.parseInt(daybirth.text.toString())){ 
       dayage= day- Integer.parseInt(daybirth.text.toString()) 
      }else { 
       dayage=0 
      } 

      val a= (yearage* 12)+ monthage 
      val b= (a * 30)+ dayage 

      val ageinyears= "$yearage years" 
      val ageinmonths= "$a months" 
      val ageindays= "$b days" 

      txtshow.text = "Your age is $yearage years, $monthage months and $dayage days" 
      txtshow2.text = "\nYOUR AGE:\nin years:\n\nin months:\n\nin days:" 
      txtshow3.text = "\n\n$ageinyears \n\n$ageinmonths \n\n $ageindays " 

      this.yearage=yearage 
      this.monthage=monthage 
      this.dayage=dayage 
     } 
    }) 
} 
} 
+2

取而代之的是崩潰的照片,至極大家都知道它看起來的樣子,儘量把一些崩潰日誌這裏 –

回答

0

您可以檢查空Input這樣的:

newString = Integer.toString(yearbirth).trim(); 
if (newString.matches("")) { 
    Toast.makeText(this, "EditText is empty", Toast.LENGTH_SHORT).show(); 
    return; 
} 
+0

我試過了並沒有工作 –

+0

然後添加崩潰報告,看看發生了什麼..在Android工作室,從設備選項卡的「工具/ android/android設備監視器」選擇您的設備,然後在'logcat'部分點擊'添加'按鈕並在'filter name'中爲您的應用程序設置一個名稱,並在'按應用程序名稱'添加您的包名稱。運行您的應用程序並查看崩潰報告。如果你正在使用另一個IDE,那麼在谷歌搜索如何獲得應用程序登錄你的IDE –

+0

謝謝我會嘗試 –

0

驗證yearbirth.text.toString()不爲空則嘗試將其解析爲一個整數之前(如果它也是一個)

爲了達到目的,請將您在代碼中顯示的行寫入:

var yearage = year - Integer.parseInt(yearbirth.text.toString()) -1 

內的if驗證輸入是有效的,就像這樣:

if(yearbirth.text.toString().matches(Regex("\\d+"))) 
    var yearage = year - Integer.parseInt(yearbirth.text.toString()) -1