【问题标题】:Edit Text shows exception Invalid Int ""编辑文本显示异常 Invalid Int ""
【发布时间】:2013-06-20 05:56:31
【问题描述】:

我已经尝试将 EditText 默认值设置为零,但是当涉及到编辑和擦除为“”(空白,编辑文本字段中没有更多的措辞 _

按下提交按钮时显示异常 Invalid Int ""。你能告诉我如何解决这个异常吗?

下面是我的代码

    for (int j = 0; j < selectedChannels; j++) {

        lconper = (Spinner) findViewById (j+120);
        int monthlyFeeforChannel = 0;       
        contractPeriodSelected.add((String) lconper.getSelectedItem());

        String monthlyTest = txtMonthlyFee[j].getText().toString().trim();
        String freeMonths = txtFreePeriod[j].getText().toString().trim();

        if(monthlyTest.trim()==null || monthlyTest.trim()==""){
            new AlertDialog.Builder(this)
            .setTitle("Warning")
            .setMessage(
                    "You must fill in Monthly Fees for subscribed channels!")
                    .setNeutralButton("OK",
                            new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int id) {
                            // if this button is clicked, close
                            // current activity
                            dialog.cancel();
                        }});
            break;
        }
        else
        {
            Log.d("Month" , monthlyTest);
            Log.d("freeMonths" , freeMonths);                               
            monthlyFeeforChannel =  Integer.parseInt(monthlyTest);                                      
            monthlyFee.add(monthlyFeeforChannel);
            FreePeriod.add(freeMonths);     
        }   
    }

Logcat

    06-20 13:52:03.711: E/AndroidRuntime(17713): FATAL EXCEPTION: main
06-20 13:52:03.711: E/AndroidRuntime(17713): java.lang.IllegalStateException: Could not execute method of the activity
06-20 13:52:03.711: E/AndroidRuntime(17713):    at android.view.View$1.onClick(View.java:3130)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at android.view.View.performClick(View.java:3652)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at android.view.View$PerformClick.run(View.java:14354)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at android.os.Handler.handleCallback(Handler.java:605)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at android.os.Looper.loop(Looper.java:137)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at android.app.ActivityThread.main(ActivityThread.java:4519)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at java.lang.reflect.Method.invokeNative(Native Method)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at java.lang.reflect.Method.invoke(Method.java:511)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at dalvik.system.NativeStart.main(Native Method)
06-20 13:52:03.711: E/AndroidRuntime(17713): Caused by: java.lang.reflect.InvocationTargetException
06-20 13:52:03.711: E/AndroidRuntime(17713):    at java.lang.reflect.Method.invokeNative(Native Method)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at java.lang.reflect.Method.invoke(Method.java:511)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at android.view.View$1.onClick(View.java:3125)
06-20 13:52:03.711: E/AndroidRuntime(17713):    ... 11 more
06-20 13:52:03.711: E/AndroidRuntime(17713): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-20 13:52:03.711: E/AndroidRuntime(17713):    at java.lang.Integer.invalidInt(Integer.java:138)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at java.lang.Integer.parseInt(Integer.java:359)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at java.lang.Integer.parseInt(Integer.java:332)
06-20 13:52:03.711: E/AndroidRuntime(17713):    at com.test.Eystem1.Confirm.submitOrder(Confirm.java:415)
06-20 13:52:03.711: E/AndroidRuntime(17713):    ... 14 more

【问题讨论】:

  • 把你的代码放在你在edittext中设置文本的地方..
  • 把空检查放到editetext。
  • rajugujarati 我同意@PankajKumar

标签: android int android-edittext


【解决方案1】:

在您使用的 aboue 代码中,例如将 String 转换为 Integer,在这种情况下,您不会将 ""(Empty String) 转换为 Integer

Integer.parseInt(monthlyTest);  // Here monthyTest value is ""

转换前请确保字符串不为空。

检查String的长度是否应该大于0。

if(monthlyTest.length()>0)

谢谢.....

【讨论】:

    【解决方案2】:

    试试

    if(monthlyTest == null || monthlyTest.trim().equals("")){
    

    你也可以使用

    if(monthlyTest == null || monthlyTest.trim().isEmpty()){
    

    【讨论】:

    • 你知道monthlyTest.trim() 如果monthlyTest 为空也可以抛出NPE 吗?检查自己,然后发布答案:)
    • 大声笑,真的,没想到这一点,这是漫长的一天。我现在编辑它。我只是查看了 "" 的无效 int
    • 完全相同的错误?这应该有望修复 if 语句,以便空字符串不会继续到您的 Integer.parseInt
    【解决方案3】:

    try/catch 块包围违规行。

    try {
        Log.d("Month" , monthlyTest);
        Log.d("freeMonths" , freeMonths);                               
        monthlyFeeforChannel =  Integer.parseInt(monthlyTest);                                      
        MonthlyFee.add(monthlyFeeforChannel);
        FreePeriod.add(freeMonths);
    }
    catch (NumberFormatException nfe) {
        nfe.printStackTrace();
        // Alert the user/log the error, etc.
    }
    

    【讨论】:

      【解决方案4】:

      我认为,您应该将其添加到您的编辑文本中

      editText.setHint("0");
      

      【讨论】:

        【解决方案5】:

        如果输入类型设置为数字,则无法测试“null”,因此最好的解决方案是测试长度是否为零,这样:

        if(number.length() == 0) {EditText is empty!!!}
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-03-20
          • 1970-01-01
          • 2014-06-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-21
          • 2010-11-29
          相关资源
          最近更新 更多