gpt4 book ai didi

android - 即使指定字段为空也处理 StringIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-03 04:55:37 26 4
gpt4 key购买 nike

enter image description here当我按下 Android 应用程序的“提交”按钮时,我遇到了应用程序崩溃的情况。该应用程序负责发送输入的数据,将其作为事件添加到用户的日历中。仅当日期字段留空时应用程序才会崩溃。我注意到存储日期分割部分的变量有一个 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);
}
}

日志猫:

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)

最佳答案

它正在按预期工作。你犯了一个错误。问题是你不能打电话subString() 在空字符串上。但是当你的字段为空时,根据代码它将尝试对 null 值调用 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));
}

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

关于android - 即使指定字段为空也处理 StringIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56576580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com