ArrayAdapter抛出ArrayIndexOutOfBoundsException

时间:2012-06-10 20:43:16

标签: android arraylist android-arrayadapter

当我使用自定义数组适配器时,我收到了一个ArrayOutOfBounds异常。

我想知道是否有任何我忽略的编码错误。

以下是错误日志:

06-10 20:21:53.254: E/AndroidRuntime(315): FATAL EXCEPTION: main
06-10 20:21:53.254: E/AndroidRuntime(315): java.lang.RuntimeException: Unable to start activity ComponentInfo{alex.android.galaxy.tab.latest/alex.android.galaxy.tab.latest.Basic_db_output}: java.lang.ArrayIndexOutOfBoundsException
06-10 20:21:53.254: E/AndroidRuntime(315):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-10 20:21:53.254: E/AndroidRuntime(315):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-10 20:21:53.254: E/AndroidRuntime(315):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-10 20:21:53.254: E/AndroidRuntime(315):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-10 20:21:53.254: E/AndroidRuntime(315):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 20:21:53.254: E/AndroidRuntime(315):  at android.os.Looper.loop(Looper.java:123)
06-10 20:21:53.254: E/AndroidRuntime(315):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-10 20:21:53.254: E/AndroidRuntime(315):  at java.lang.reflect.Method.invokeNative(Native Method)
06-10 20:21:53.254: E/AndroidRuntime(315):  at java.lang.reflect.Method.invoke(Method.java:521)
06-10 20:21:53.254: E/AndroidRuntime(315):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-10 20:21:53.254: E/AndroidRuntime(315):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-10 20:21:53.254: E/AndroidRuntime(315):  at dalvik.system.NativeStart.main(Native Method)
06-10 20:21:53.254: E/AndroidRuntime(315): Caused by: java.lang.ArrayIndexOutOfBoundsException
06-10 20:21:53.254: E/AndroidRuntime(315):  at alex.android.galaxy.tab.latest.Basic_db_output.onCreate(Basic_db_output.java:44)
06-10 20:21:53.254: E/AndroidRuntime(315):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-10 20:21:53.254: E/AndroidRuntime(315):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

我的代码示例基于:How to use ArrayAdapter<myClass>

ArrayList<Question> list = new ArrayList<Question>();

for(int i=1; i <= 3; i++)
{       
    path = getAssets().open("quiz"+i+".txt");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Reader reader = new ResultsReader(path);
    reader.read();

    String str = ((ResultsReader)reader).getInput();
    String data[] = str.split("<.>");

    Question q = new Question();

    q.question = data[0];
    q.answer = Integer.parseInt(data[1]);
    q.choice1 = data[2];
    q.choice2 = data[3];
    q.choice3 = data[4];
    list.add(q);
}

1 个答案:

答案 0 :(得分:1)

当你这样做时

 String data[] = str.split("<.>");

它将返回一个不确定长度的数组,除非你知道输入是正确的,当你执行以下操作时,你将遇到问题:

    q.question = data[0];
    q.answer = Integer.parseInt(data[1]);
    q.choice1 = data[2];
    q.choice2 = data[3];
    q.choice3 = data[4];

也许你应该想到另一种技术,你不依赖于调用数组的显式索引。

例如:

if(data.length == 4){
        q.question = data[0];
        q.answer = Integer.parseInt(data[1]);
        q.choice1 = data[2];
        q.choice2 = data[3];
        q.choice3 = data[4];
} else {
   Log.e("YourApp", "Data was not split from file correctly");
}

或在使用拆分数据阵列之前添加一些调试:

for(int i=0; i < data.length; i++){
   Log.d("YourApp", "DataReceived:"+data[i];
}

修改

哦,是的,问题将是模拟器/你的手机还没有找到什么

 C:/