Android 如何处理arraylist的ArrayIndexOutOfBoundsException
我是android的新手。在我的应用程序中,在某些情况下会发生ArrayIndexOutOfBoundsException。但我不能复制相同的。有人能帮我解决这个异常吗 LogcatAndroid 如何处理arraylist的ArrayIndexOutOfBoundsException,android,indexoutofboundsexception,Android,Indexoutofboundsexception,我是android的新手。在我的应用程序中,在某些情况下会发生ArrayIndexOutOfBoundsException。但我不能复制相同的。有人能帮我解决这个异常吗 Logcat Non-fatal Exception: java.lang.IndexOutOfBoundsException: Invalid index 6, size is 6 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:2
Non-fatal Exception: java.lang.IndexOutOfBoundsException: Invalid index 6, size is 6
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.beco.ibeco.model.Store.getOpenHoursString(Store.java:209)
at com.beco.ibeco.app.store.StoreDetailFragment.populateStoreViews(StoreDetailFragment.java:491)
at com.beco.ibeco.app.store.StoreDetailFragment.access$700(StoreDetailFragment.java:99)
at com.beco.ibeco.app.store.StoreDetailFragment$3.onLoadFinished(StoreDetailFragment.java:408)
at com.beco.ibeco.app.store.StoreDetailFragment$3.onLoadFinished(StoreDetailFragment.java:386)
at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:479)
at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:447)
at android.support.v4.content.Loader.deliverResult(Loader.java:126)
at com.beco.ibeco.content.ModelLoader.deliverResult(ModelLoader.java:61)
at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:249)
at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:77)
at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:476)
at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:493)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
这就是代码
public String getOpenHoursString() {
MutableDateTime now = MutableDateTime.now();
int dayIndex = now.getDayOfWeek() - 1;
if (hours != null) {
OpeningHour bopHours = hours.get(dayIndex);
if (bopHours != null && bopHours.openTime != null && bopHours.closeTime != null) {
DateTimeZone timeZone = DateTimeZone.getDefault();
DateTimeFormatter format = DateTimeFormat.forPattern("h:mm a");
DateTimeFormatter parseFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZoneUTC();
DateTime open = DateTime.parse(bopHours.openTime, parseFormat).toDateTime(timeZone);
DateTime close = DateTime.parse(bopHours.closeTime, parseFormat).toDateTime(timeZone);
if ((now.getHourOfDay() >= open.getHourOfDay()) && (now.getHourOfDay() < close.getHourOfDay())) {
return String.format("Open till - %s", format.print(close)).toLowerCase();
} else {
return String.format("%s - %s", format.print(open), format.print(close)).toLowerCase();
}
}
}
return "";
}
public String getOpenCloseString(int type){
Map<Integer,String> mOpeningHours = new LinkedHashMap<>();
for (OpeningHour bopHours : hours) {
DateTimeZone timeZone = DateTimeZone.getDefault();
DateTimeFormatter format = DateTimeFormat.forPattern("h:mm a");
DateTimeFormatter parseFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZoneUTC();
DateTime open = DateTime.parse(bopHours.openTime, parseFormat).toDateTime(timeZone);
DateTime close = DateTime.parse(bopHours.closeTime, parseFormat).toDateTime(timeZone);
mOpeningHours.put(bopHours.day,String.format("%s - %s", format.print(open), format.print(close)).toUpperCase());
}
return mOpeningHours.get(type) == null?"closed":mOpeningHours.get(type);
}
这是来自服务器的arraylist的示例响应
hours: [
{
close_time: "2017-05-03T16:30:00Z",
day: 1,
open_time: "2017-05-03T06:00:00Z"
},
{
close_time: "2017-05-03T16:30:00Z",
day: 2,
open_time: "2017-05-03T06:00:00Z"
},
{
close_time: "2017-05-03T16:30:00Z",
day: 3,
open_time: "2017-05-03T06:00:00Z"
},
{
close_time: "2017-05-03T16:30:00Z",
day: 4,
open_time: "2017-05-03T06:00:00Z"
},
{
close_time: "2017-05-03T16:30:00Z",
day: 5,
open_time: "2017-05-03T06:00:00Z"
},
{
close_time: "2017-05-03T16:30:00Z",
day: 6,
open_time: "2017-05-03T06:00:00Z"
},
{
close_time: "2017-05-03T16:30:00Z",
day: 7,
open_time: "2017-05-03T06:00:00Z"
}
]
编辑
我在周日得到这些例外情况(这将使dayIndex为7-1=6)
对于一些商店,我得到了响应
hours: [
{
close_time: "2017-05-03T15:00:00Z",
day: 2,
open_time: "2017-05-03T04:30:00Z"
},
{
close_time: "2017-05-03T15:00:00Z",
day: 3,
open_time: "2017-05-03T04:30:00Z"
},
{
close_time: "2017-05-03T15:00:00Z",
day: 4,
open_time: "2017-05-03T04:30:00Z"
},
{
close_time: "2017-05-03T15:00:00Z",
day: 5,
open_time: "2017-05-03T04:30:00Z"
},
{
close_time: "2017-05-03T15:00:00Z",
day: 6,
open_time: "2017-05-03T04:30:00Z"
},
{
close_time: "2017-05-03T15:00:00Z",
day: 7,
open_time: "2017-05-03T04:30:00Z"
}
]
有人帮我吗?提前谢谢 您的代码中似乎有一些地方没有正确初始化 但是我的钱就在上面了 int dayIndex=now.getDayOfWeek()-1; 可能会导致一个负值,并在这里产生一些caca OpeningHour bopHours=hours.get(dayIndex)
放置一些断点,检查这些值和/或转储,然后记录并返回给我们;-) 您的代码中似乎有一些地方没有正确初始化 但是我的钱就在上面了 int dayIndex=now.getDayOfWeek()-1; 可能会导致一个负值,并在这里产生一些caca OpeningHour bopHours=hours.get(dayIndex)
放置一些断点,检查这些值和/或转储,然后记录并返回给我们;-) 我忘了检查arrayList的大小
hours
是否小于dayIndex
public String getOpenHoursString() {
MutableDateTime now = MutableDateTime.now();
int dayIndex = now.getDayOfWeek() - 1;
if (hours != null && hours.length>dayIndex) {
OpeningHour bopHours = hours.get(dayIndex);
if (bopHours != null && bopHours.openTime != null && bopHours.closeTime != null) {
DateTimeZone timeZone = DateTimeZone.getDefault();
DateTimeFormatter format = DateTimeFormat.forPattern("h:mm a");
DateTimeFormatter parseFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZoneUTC();
DateTime open = DateTime.parse(bopHours.openTime, parseFormat).toDateTime(timeZone);
DateTime close = DateTime.parse(bopHours.closeTime, parseFormat).toDateTime(timeZone);
if ((now.getHourOfDay() >= open.getHourOfDay()) && (now.getHourOfDay() < close.getHourOfDay())) {
return String.format("Open till - %s", format.print(close)).toLowerCase();
} else {
return String.format("%s - %s", format.print(open), format.print(close)).toLowerCase();
}
}
}
return "";
}
公共字符串getOpenHoursString(){
MutableDateTime now=MutableDateTime.now();
int dayIndex=now.getDayOfWeek()-1;
if(小时数!=null&&hours.length>dayIndex){
OpeningHour bopHours=hours.get(dayIndex);
if(bopHours!=null&&bopHours.openTime!=null&&bopHours.closeTime!=null){
DateTimeZone timeZone=DateTimeZone.getDefault();
DateTimeFormatter format=DateTimeFormat.forPattern(“h:mm a”);
DateTimeFormatter parseFormat=DateTimeFormat.forPattern(“yyyy-MM-dd'T'HH:MM:ss'Z'))。withZoneUTC();
DateTime open=DateTime.parse(bopHours.openTime,parseFormat).toDateTime(时区);
DateTime close=DateTime.parse(bopHours.closeTime,parseFormat).toDateTime(时区);
如果((now.getHourOfDay()>=open.getHourOfDay())&&(now.getHourOfDay()
现在没事了我忘了检查arrayList的大小
hours
是否小于dayIndex
public String getOpenHoursString() {
MutableDateTime now = MutableDateTime.now();
int dayIndex = now.getDayOfWeek() - 1;
if (hours != null && hours.length>dayIndex) {
OpeningHour bopHours = hours.get(dayIndex);
if (bopHours != null && bopHours.openTime != null && bopHours.closeTime != null) {
DateTimeZone timeZone = DateTimeZone.getDefault();
DateTimeFormatter format = DateTimeFormat.forPattern("h:mm a");
DateTimeFormatter parseFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZoneUTC();
DateTime open = DateTime.parse(bopHours.openTime, parseFormat).toDateTime(timeZone);
DateTime close = DateTime.parse(bopHours.closeTime, parseFormat).toDateTime(timeZone);
if ((now.getHourOfDay() >= open.getHourOfDay()) && (now.getHourOfDay() < close.getHourOfDay())) {
return String.format("Open till - %s", format.print(close)).toLowerCase();
} else {
return String.format("%s - %s", format.print(open), format.print(close)).toLowerCase();
}
}
}
return "";
}
公共字符串getOpenHoursString(){
MutableDateTime now=MutableDateTime.now();
int dayIndex=now.getDayOfWeek()-1;
if(小时数!=null&&hours.length>dayIndex){
OpeningHour bopHours=hours.get(dayIndex);
if(bopHours!=null&&bopHours.openTime!=null&&bopHours.closeTime!=null){
DateTimeZone timeZone=DateTimeZone.getDefault();
DateTimeFormatter format=DateTimeFormat.forPattern(“h:mm a”);
DateTimeFormatter parseFormat=DateTimeFormat.forPattern(“yyyy-MM-dd'T'HH:MM:ss'Z'))。withZoneUTC();
DateTime open=DateTime.parse(bopHours.openTime,parseFormat).toDateTime(时区);
DateTime close=DateTime.parse(bopHours.closeTime,parseFormat).toDateTime(时区);
如果((now.getHourOfDay()>=open.getHourOfDay())&&(now.getHourOfDay()
现在可以了数组索引的可能重复项从0开始。在编写这么多代码之前,您需要修改Java概念:基本上,您希望列表中的条目至少与一周中的天数相同,但事实并非如此。您的列表中只有6个条目,因此,当您试图获取一周的最后一天时,您已经超出了边界。因此,您从服务器获取的数据有问题。或者您没有正确构建“小时”列表。数组索引的可能重复项从0开始。在编写这么多代码之前,您需要修改Java概念:基本上,您希望列表中的条目至少与一周中的天数相同,但事实并非如此。您的列表中只有6个条目,因此,当您试图获取一周的最后一天时,您已经超出了边界。因此,您从服务器获取的数据有问题。或者你没有正确地建立你的“小时”列表。