即使指定的字段为空,也要处理StringIndexOutOfBoundsException

时间:2019-06-13 08:37:18

标签: android crash

enter image description here,当我按下android应用程序的Submit按钮时发生应用崩溃。 该应用程序处理发送输入的数据以作为事件添加到用户日历中的问题。 仅当日期字段留空时,应用程序才会崩溃。 我注意到对于存储日期拆分部分的变量有一个java.lang.StringIndexOutOfBoundsException。我尝试注释日期字段数据输入,然后该应用程序正常工作。

如果将上述字段留空,为什么会发生此异常?

任何修复将不胜感激。

代码:

public void onClick(View view) {

        TextInputEditText textInputEditTextDate = (TextInputEditText) findViewById(R.id.date);
        EditText editTextTitle = (EditText) findViewById(R.id.title);
        EditText editTextDesc = (EditText) findViewById(R.id.desc);
        EditText editTextLocation = (EditText) findViewById(R.id.location);
        titl = editTextTitle.getText().toString();
        desc = editTextDesc.getText().toString();
        location = editTextLocation.getText().toString();
        date = textInputEditTextDate.getText().toString();
        day = Integer.parseInt(date.substring(0, 2));
        month = Integer.parseInt(date.substring(3, 5)) - 1;
        year = Integer.parseInt(date.substring(6, 10));

        if (titl.isEmpty() || desc.isEmpty() || location.isEmpty() || date.isEmpty()) {
            Toast.makeText(this, "No information", Toast.LENGTH_SHORT).show();
        } else {
            Intent calendarIntent = new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI);
            calendarIntent.putExtra(CalendarContract.Events.TITLE, titl);
            calendarIntent.putExtra(CalendarContract.Events.EVENT_LOCATION, location);
            calendarIntent.putExtra(CalendarContract.Events.DESCRIPTION, desc);
            calendarIntent.putExtra(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_DEFAULT);
            calendarIntent.putExtra(CalendarContract.Reminders.MINUTES, 1);
            calendarIntent.putExtra(String.valueOf(CalendarContract.Reminders.METHOD_ALERT), "1");
            GregorianCalendar date = new GregorianCalendar(year, month, day);
            calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, date.getTimeInMillis());
            calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, date.getTimeInMillis());
            startActivity(calendarIntent);
        }
    }

Logcat:

Process: com.cypherlabs.calendarapp, PID: 32057
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
        at android.view.View.performClick(View.java:7333)
        at android.widget.TextView.performClick(TextView.java:14160)
        at android.view.View.performClickInternal(View.java:7299)
        at android.view.View.access$3200(View.java:846)
        at android.view.View$PerformClick.run(View.java:27773)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:6981)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
        at android.view.View.performClick(View.java:7333) 
        at android.widget.TextView.performClick(TextView.java:14160) 
        at android.view.View.performClickInternal(View.java:7299) 
        at android.view.View.access$3200(View.java:846) 
        at android.view.View$PerformClick.run(View.java:27773) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:6981) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445) 
     Caused by: java.lang.StringIndexOutOfBoundsException: length=0; index=2
        at java.lang.String.substring(String.java:2032)
        at com.cypherlabs.calendarapp.MainActivity.onClick(MainActivity.java:34)
        at java.lang.reflect.Method.invoke(Native Method) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
        at android.view.View.performClick(View.java:7333) 
        at android.widget.TextView.performClick(TextView.java:14160) 
        at android.view.View.performClickInternal(View.java:7299) 
        at android.view.View.access$3200(View.java:846) 
        at android.view.View$PerformClick.run(View.java:27773) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:6981) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)

2 个答案:

答案 0 :(得分:0)

由于此行,您将获得异常:

day = Integer.parseInt(date.substring(0, 2));

第二个参数大于String的长度,因此substring方法引发异常。

这是子字符串方法,您可以在其中检查其引发异常的原因:

public String substring(int beginIndex, int endIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(this, beginIndex);
        }
        if (endIndex > length()) {
            throw new StringIndexOutOfBoundsException(this, endIndex);
        }
        int subLen = endIndex - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }

        return ((beginIndex == 0) && (endIndex == length())) ? this
                : fastSubstring(beginIndex, subLen);
}

在处理date字符串之前,我会检查它的长度。由于hint的格式为DD/MM/YYYY,因此您应确保该字符串包含10个字符。

答案 1 :(得分:0)

它正在按预期方式工作。你做错了问题是你不能打电话 空字符串上的subString()。但是,根据代码,当您的字段为空时 它将尝试在空值上调用subString()方法。

可以通过以下方式解决。

if(date!=null && !date.isEmpty()){
     day = Integer.parseInt(date.substring(0, 2));
     month = Integer.parseInt(date.substring(3, 5)) - 1;
     year = Integer.parseInt(date.substring(6, 10));
}

请记住,每次需要用户输入时,所有方式都要遵循这些验证。