java.lang.IndexOutOfBoundsException:索引1无效,大小为1

时间:2013-03-26 16:05:46

标签: java android arraylist

我必须开发一个Android应用程序。我在这里得到以下错误:

03-26 21:29:00.394: E/AndroidRuntime(684): FATAL EXCEPTION: main
03-26 21:29:00.394: E/AndroidRuntime(684): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
03-26 21:29:00.394: E/AndroidRuntime(684):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
03-26 21:29:00.394: E/AndroidRuntime(684):  at java.util.ArrayList.get(ArrayList.java:311)
03-26 21:29:00.394: E/AndroidRuntime(684):  at com.xmlparsing.MainActivity.loadXMLData(MainActivity.java:303)
03-26 21:29:00.394: E/AndroidRuntime(684):  at com.xmlparsing.MainActivity.access$2(MainActivity.java:118)
03-26 21:29:00.394: E/AndroidRuntime(684):  at com.xmlparsing.MainActivity$GetXmlFiles.onPostExecute(MainActivity.java:369)
03-26 21:29:00.394: E/AndroidRuntime(684):  at com.xmlparsing.MainActivity$GetXmlFiles.onPostExecute(MainActivity.java:1)
03-26 21:29:00.394: E/AndroidRuntime(684):  at android.os.AsyncTask.finish(AsyncTask.java:417)
03-26 21:29:00.394: E/AndroidRuntime(684):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
03-26 21:29:00.394: E/AndroidRuntime(684):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
03-26 21:29:00.394: E/AndroidRuntime(684):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-26 21:29:00.394: E/AndroidRuntime(684):  at android.os.Looper.loop(Looper.java:123)
03-26 21:29:00.394: E/AndroidRuntime(684):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-26 21:29:00.394: E/AndroidRuntime(684):  at java.lang.reflect.Method.invokeNative(Native Method)
03-26 21:29:00.394: E/AndroidRuntime(684):  at java.lang.reflect.Method.invoke(Method.java:521)
03-26 21:29:00.394: E/AndroidRuntime(684):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-26 21:29:00.394: E/AndroidRuntime(684):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-26 21:29:00.394: E/AndroidRuntime(684):  at dalvik.system.NativeStart.main(Native Method)

这里我使用了以下代码:

String[] stockArrVideo = new String[Appscontent.Sub_arraylistvideo.size()];
stockArrVideo = Appscontent.Sub_arraylistvideo.toArray(stockArrVideo);
for (String s : stockArrVideo) {
    if (s.startsWith(number)) {
        String _Substring;
        _Substring = s.substring(1);
        if(_Substring.equals("")||_Substring == null) {
            _Substring = "No Records";
        }
        Appscontent.Sub_arraylistvideosub.add(_Substring);
    }
    h++;
}           

如果数据为空,则表示我必须显示“无记录”消息。否则显示正确的数据。

TextView mVideo = new TextView(this); 
System.out.println(Appscontent.Sub_arraylistvideosub.get(j));
mVideo.setText(Appscontent.Sub_arraylistvideosub.get(j));
mVideo.setLayoutParams(textLayoutParams);
mVideo.setBackgroundColor(Color.BLACK);

在这里,我编写了用于检查空数据的代码。

导致此例外的原因是什么?

1 个答案:

答案 0 :(得分:6)

IndexOutOfBoundsException: Invalid index 1, size is 1
  

我该如何解决这些错误???

您需要检查阵列的大小,问题似乎发生在:Appscontent.Sub_arraylistvideosub.get(j) 在尝试使用之前,只需检查您的索引是否有效,例如:

if(j < Appscontent.Sub_arraylistvideosub.length)
    System.out.println(Appscontent.Sub_arraylistvideosub.get(j));