2016-09-30 65 views
-4

如果用戶什麼都不輸入,並點擊ok按鈕,應用程序應該顯示一個烤麪包輸入東西。但是,當我開始應用,然後點擊確定按鈕,而無需輸入,應用程序崩潰顯示此錯誤:應用程序崩潰而不是顯示空的EditText烤麪包

09-30 05:06:59.099: E/AndroidRuntime(1656): FATAL EXCEPTION: main 
09-30 05:06:59.099: E/AndroidRuntime(1656): Process: com.example.mybalancesheet, PID: 1656 
09-30 05:06:59.099: E/AndroidRuntime(1656): java.lang.IllegalStateException: Could not execute method for android:onClick 
09-30 05:06:59.099: E/AndroidRuntime(1656):  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) 
09-30 05:06:59.099: E/AndroidRuntime(1656):  at android.view.View.performClick(View.java:4438) 
09-30 05:06:59.099: E/AndroidRuntime(1656):  at android.view.View$PerformClick.run(View.java:18422) 
09-30 05:06:59.099: E/AndroidRuntime(1656):  at android.os.Handler.handleCallback(Handler.java:733) 
09-30 05:06:59.099: E/AndroidRuntime(1656):  at android.os.Handler.dispatchMessage(Handler.java:95) 
09-30 05:06:59.099: E/AndroidRuntime(1656):  at android.os.Looper.loop(Looper.java:136) 
09-30 05:06:59.099: E/AndroidRuntime(1656):  at android.app.ActivityThread.main(ActivityThread.java:5001) 
09-30 05:06:59.099: E/AndroidRuntime(1656):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-30 05:06:59.099: E/AndroidRuntime(1656):  at java.lang.reflect.Method.invoke(Method.java:515) 
09-30 05:06:59.099: E/AndroidRuntime(1656):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
09-30 05:06:59.099: E/AndroidRuntime(1656):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
09-30 05:06:59.099: E/AndroidRuntime(1656):  at dalvik.system.NativeStart.main(Native Method) 
09-30 05:06:59.099: E/AndroidRuntime(1656): Caused by: java.lang.reflect.InvocationTargetException 
09-30 05:06:59.099: E/AndroidRuntime(1656):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-30 05:06:59.099: E/AndroidRuntime(1656):  at java.lang.reflect.Method.invoke(Method.java:515) 
09-30 05:06:59.099: E/AndroidRuntime(1656):  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
09-30 05:06:59.099: E/AndroidRuntime(1656):  ... 11 more 

更令人困惑的是,我有一個清除所有按鈕,重置一切的開始。現在,如果我此時單擊此按鈕而不輸入任何內容,則會顯示敬酒。

我無法理解爲什麼。在EditText上輸入第一次點擊:

enter image description here

單擊全部清除後:

enter image description here

enter image description here

還有一兩件事,如果我嘗試後明確進入東西所有,然後再次點擊ok按鈕時,應用程序崩潰。

代碼:

​​

此錯誤顯示當我點擊空的EditText:

09-30 05:10:37.069: E/AndroidRuntime(1741): FATAL EXCEPTION: main 
09-30 05:10:37.069: E/AndroidRuntime(1741): Process: com.example.mybalancesheet, PID: 1741 
09-30 05:10:37.069: E/AndroidRuntime(1741): java.lang.IllegalStateException: Could not execute method for android:onClick 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at android.view.View.performClick(View.java:4438) 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at android.view.View$PerformClick.run(View.java:18422) 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at android.os.Handler.handleCallback(Handler.java:733) 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at android.os.Handler.dispatchMessage(Handler.java:95) 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at android.os.Looper.loop(Looper.java:136) 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at android.app.ActivityThread.main(ActivityThread.java:5001) 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at java.lang.reflect.Method.invoke(Method.java:515) 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at dalvik.system.NativeStart.main(Native Method) 
09-30 05:10:37.069: E/AndroidRuntime(1741): Caused by: java.lang.reflect.InvocationTargetException 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at java.lang.reflect.Method.invoke(Method.java:515) 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
09-30 05:10:37.069: E/AndroidRuntime(1741):  ... 11 more 
09-30 05:10:37.069: E/AndroidRuntime(1741): Caused by: java.lang.NumberFormatException: Invalid int: "" 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at java.lang.Integer.invalidInt(Integer.java:137) 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at java.lang.Integer.parseInt(Integer.java:358) 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at java.lang.Integer.parseInt(Integer.java:331) 
09-30 05:10:37.069: E/AndroidRuntime(1741):  at com.example.mybalancesheet.MainActivity.salaryEntered(MainActivity.java:116) 
09-30 05:10:37.069: E/AndroidRuntime(1741):  ... 14 more 

我的XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textVProm" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Enter ur Salary" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <EditText 
     android:id="@+id/edEnteredSal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="number" > 

     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/buttOk" 
     android:layout_gravity="center" 
     android:onClick="salaryEntered" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="OK" /> 
<TextView 
     android:id="@+id/tvSal" 
     android:layout_width="wrap_content" 
     android:layout_marginTop="34dp" 
     android:layout_gravity="center" 

     android:layout_height="wrap_content" 
     android:text="0" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginTop="23dp" 
     android:onClick="goTo" 
     android:layout_height="wrap_content" 
     android:text="New Entry" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
      android:layout_gravity="center" 
      android:onClick="clear" 
     android:layout_height="wrap_content" 
     android:text="Clear All" /> 


</LinearLayout> 
+0

你可以用'etName。addTextChangedListener(新TextWatcher(){ \t \t \t \t \t @Override \t \t \t \t \t公共無效beforeTextChanged(CharSequence中的CharSequence,INT I,INT I1,I2 INT){ \t \t \t \t \t \t \t \t \t \t \t} \t \t \t \t \t @Override \t \t \t \t \t公共無效onTextChanged(CharSequence中的CharSequence,INT I,INT I1,I2 INT){ \t \t \t \t \t} \t \t \t \t \t @Override \t \t \t \t \t public void afterTextChanged(Editable editable){ \t \t \t \t \t} \t \t \t \t});'與**的EditText ** –

+0

崗位工作的XML文件。 –

+0

請發佈您的佈局文件 – Bmuig

回答

0

你comparization或零檢查是錯誤的。在您的salaryEntered()

if(sal.contentEquals(" ")){ 

你應該利用

TextUtils.isEmpty() 

代替。例如,如果EditText上是空的,然後你用你的原始狀態,它看起來就像這樣:

"" == " " 

- 這是FALSE,這將使它轉到else塊,並嘗試解析空值通過調用

saly=Integer.parseInt(sal); 

,讓錯誤:

Caused by: java.lang.NumberFormatException: Invalid int: "" 
+0

@utkarshdubey嘗試將if(sal.contentEquals(「」))'替換爲if(TextUtils.isEmpty(sal))'。 –

+0

它還在! –

+0

它第一次工作,但是當我在'clear All'後再次點擊'ok'按鈕時,同樣的錯誤出現了! –

相關問題