Array = List <array> .get(int):程序崩溃

时间:2016-07-11 08:49:36

标签: java android crash

为什么这段代码会破坏程序?

NormalListII = Enemy.NormalLootsListII.get(PostionToDelete);

还有更多代码:

public class One
{
    int PostionToGet = 0;
    Loots[] NormalListII = new Loots[] { };
    public void switch() 
    { 
        (...)
        Enemy enemy;
        NormalListII = Enemy.MyLoots1.get(PostionToGet);
    }
}

public class Enemy
{
    public static ArrayList<Loots[]> MyLoots1 = new ArrayList<Loots[]>();
    public static ArrayList<Loots[]> MyLoots2 = new ArrayList<Loots[]>();
    public static ArrayList<Loots[]> MyLoots3 = new ArrayList<Loots[]>();
    public static ArrayList<Loots[]> MyLoots4 = new ArrayList<Loots[]>();
    //Loots is a class.  

    public Loots[] ToList1 = new Loots[] {new Loots(1, "7"), (...) };
    public Loots[] ToList2 = new Loots[] {new Loots(2, "48"), (...) };
    public Loots[] ToList3 = new Loots[] {new Loots(3, "29"), (...) };
    public Loots[] ToList4 = new Loots[] {new Loots(4, "16"), (...) };

public Enemy() 
{
    MyLoots1.add(ToList1);
    MyLoots2.add(ToList2);
    MyLoots3.add(ToList3);
    MyLoots4.add(ToList4);   
}

我不知道为什么这会导致程序崩溃。

我在debbuger和这条线上吮吸程序

(NormalListII = Enemy.NormalLootsListII.get(PostionToDelete);)  

程序正在崩溃。

1 个答案:

答案 0 :(得分:1)

你必须调用Enemy类的构造函数,因为你已经将项目添加到数组列表中,在这里你正在访问0位置的元素,但实际上arraylist没有任何项目因此导致崩溃