为什么我的应用程序在设置数组后崩溃了?

时间:2014-03-30 21:51:15

标签: java android arrays crash

我的应用程序一直使用数组。但是我只在创建应用程序后设置了值:

      public String[][] stunde = new String [6][13];
      public String[][] lehrer = new String [6][13];


    stunde[1][1]= "SZ";
    stunde[2][1]= "Bi";
    stunde[3][1]= "";
    stunde[4][1]= "DG2";
    stunde[5][1]= "";


    lehrer[1][1]= "Gt";
    lehrer[2][1]= "Pön";
    lehrer[3][1]= "";
    lehrer[4][1]= "Lc";
    lehrer[5][1]= "";

但现在我想在之前设置这些值,以便我可以在另一种方法中使用它们。 像这样:

  public  String[][] stunde = {
            {"SZ", "SZ", "Ku", "Ku", "M", "M", "GeL1", "EL2"},
           {"Bi", "Bi", "EL2", "EL2", "Pl", "Pl","DG2","If"},
            {"", "", "EL2","EL2", "","","M","Bi"},
            {"DG2", "DG2", "","", "GeL1","GeL1","Pl","Ku"},
            {"", "", "GeL1","GeL1", "If","If","","SZ","","","Sp","Sp"}
    };

但是在我尝试了第二个代码后,我的应用程序在打开后开始崩溃。

任何想法为什么?

1 个答案:

答案 0 :(得分:3)

您的旧代码使用从一级开始的顶级数组的索引,而不是零。看起来您的应用程序的其余部分也依赖于该编号。

添加"假冒"行和列来解决问题:

public  String[][] stunde = {
/*0*/  {"", "", "", "", "", "", "", "", ""},
/*1*/  {"", "SZ", "SZ", "Ku", "Ku", "M", "M", "GeL1", "EL2"},
/*2*/  {"", "Bi", "Bi", "EL2", "EL2", "Pl", "Pl","DG2","If"},
/*3*/  {"", "", "", "EL2","EL2", "","","M","Bi"},
/*4*/  {"", "DG2", "DG2", "","", "GeL1","GeL1","Pl","Ku"},
/*5*/  {"", "", "", "GeL1","GeL1", "If","If","","SZ","","","Sp","Sp"}
};

现在,您的行0仍然未使用,而您希望行15的应用的其余部分会找到以前的数据。列编号也是如此。

注意:一旦你控制了这个并且应用程序不再崩溃,长期的方法是改变使用数组索引0..4而不是1..5的代码。< / p>