ある配列リストから別の配列リストにアイテムを削除して配置すると、この例外が発生し続けます。インデックスが 13 でサイズが 5 の場合もあれば、0 と 0 の場合もあります。これはランダムに発生し、低速のデバイスでより頻繁に発生します。
11-05 19:42:34.487: W/dalvikvm(2232): threadid=14: thread exiting with uncaught exception (group=0x40c3ca68)
11-05 19:42:34.497: E/AndroidRuntime(2232): FATAL EXCEPTION: Thread-491
11-05 19:42:34.497: E/AndroidRuntime(2232): java.lang.IndexOutOfBoundsException: Invalid index size is 5
11-05 19:42:34.497: E/AndroidRuntime(2232): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
11-05 19:42:34.497: E/AndroidRuntime(2232): at java.util.ArrayList.add(ArrayList.java:143)
11-05 19:42:34.497: E/AndroidRuntime(2232): at com.pigeoncraft.pigeoncraft.entity.Inventory.add(Inventory.java:25)
11-05 19:42:34.497: E/AndroidRuntime(2232): at com.pigeoncraft.pigeoncraft.screen.ContainerMenu.tick(ContainerMenu.java:62)
11-05 19:42:34.497: E/AndroidRuntime(2232): at com.pigeoncraft.pigeoncraft.Game.tick(Game.java:312)
11-05 19:42:34.497: E/AndroidRuntime(2232): at com.pigeoncraft.pigeoncraft.Game.iterate(Game.java:265)
11-05 19:42:34.497: E/AndroidRuntime(2232): at com.pigeoncraft.pigeoncraft.GameActivity$2.run(GameActivity.java:128)
11-05 19:42:34.497: E/AndroidRuntime(2232): at java.lang.Thread.run(Thread.java:856)
これが Inventory のコードです。
package com.mojang.ld22.entity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.pigeoncraft.pigeoncraft.item.Item;
import com.pigeoncraft.pigeoncraft.ResourceItem;
import com.pigeoncraft.pigeoncraft.Resource;
public class Inventory implements Serializable {
private static final long serialVersionUID = -8246630353617240883L;
public List<Item> items = new ArrayList<Item>();
public void add(Item item) {
add(items.size(), item);
}
public void add(int slot, Item item) {
if (item instanceof ResourceItem) {
ResourceItem toTake = (ResourceItem) item;
ResourceItem has = findResource(toTake.resource);
if (has == null) {
items.add(slot, toTake); <--------------------
} else {
has.count += toTake.count;
}
} else {
items.add(slot, item);
}
}
private ResourceItem findResource(Resource resource) {
for (int i = 0; i < items.size(); i++) {
if (items.get(i) instanceof ResourceItem) {
ResourceItem has = (ResourceItem) items.get(i);
if (has.resource.name.equals(resource.name))
{
return has;
}
}
}
return null;
}
public boolean hasResources(Resource r, int count) {
ResourceItem ri = findResource(r);
if (ri == null) return false;
return ri.count >= count;
}
public boolean removeResource(Resource r, int count) {
ResourceItem ri = findResource(r);
if (ri == null) return false;
if (ri.count < count) return false;
ri.count -= count;
if (ri.count <= 0) items.remove(ri);
return true;
}
public int count(Item item) {
if (item instanceof ResourceItem) {
ResourceItem ri = findResource(((ResourceItem)item).resource);
if (ri!=null) return ri.count;
} else {
int count = 0;
for (int i=0; i<items.size(); i++) {
if (items.get(i).matches(item)) count++;
}
return count;
}
return 0;
}
}