我是android的新手。在我的应用程序中,在某些情况下会发生ArrayIndexOutOfBoundsException。但我无法再现相同的内容。任何人都可以帮我修复此异常 的 logcat的
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);
}
populateStoreViews()
private void populateStoreViews(Store store) {
if (store == null || mWideImageView == null || mStoreLogo == null) {
return;
}
if (store.isFavourite() && Beco.getApp().currentUser().isSignedIn()) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_favorite));
} else {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_favourite_unselected_15dp));
}
setTitle(store.getName());
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(store.getImageUri(Store.IMAGE_WIDE, Beco.getApp().getDensity()))
.setProgressiveRenderingEnabled(true)
.build();
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
.setOldController(mWideImageView.getController())
.build();
GenericDraweeHierarchyBuilder builder =
new GenericDraweeHierarchyBuilder(getContext().getResources());
GenericDraweeHierarchy hierarchy = builder
.setFadeDuration(200)
.setPlaceholderImage(ContextCompat.getDrawable(getContext(), R.drawable.image_mall_default))
.build();
mWideImageView.setHierarchy(hierarchy);
mWideImageView.setController(controller);
containerStoreLogo.setCardBackgroundColor(store.getColor());
ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(store.getImageUri(Store.IMAGE_SQ, Beco.getApp().getDensity()))
.setProgressiveRenderingEnabled(true)
.build();
DraweeController draweeController = Fresco.newDraweeControllerBuilder()
.setImageRequest(imageRequest)
.setOldController(mStoreLogo.getController())
.build();
GenericDraweeHierarchyBuilder genericDraweeHierarchyBuilder =
new GenericDraweeHierarchyBuilder(getContext().getResources());
GenericDraweeHierarchy genericDraweeHierarchy = genericDraweeHierarchyBuilder
.setFadeDuration(200)
.setPlaceholderImage(ContextCompat.getDrawable(getContext(), R.drawable.img_store_loader))
.build();
mStoreLogo.setHierarchy(genericDraweeHierarchy);
mStoreLogo.setController(draweeController);
mAddressView.setText(store.getName());
mStatusView.setText(store.getOpenHoursString());/*this line*/
mDistanceView.setText(getResources().getString(R.string.distance_string, store.getDistanceString(Beco.getApp().getLocation())));
}
这是来自服务器的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"
}
]
请有人帮帮我吗?提前谢谢!
答案 0 :(得分:0)
似乎代码小时中的某个位置未正确初始化。
但我的钱会在 int dayIndex = now.getDayOfWeek() - 1; 可能导致负值,并在这里做一些caca OpeningHour bopHours = hours.get(dayIndex);
放置一些断点,检查这些值和/或转储然后转到Log并返回给我们; - )
答案 1 :(得分:0)
我忘了检查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 "";
}
现在没关系