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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15641805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:22:51  来源:igfitidea点击:

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1

javaandroidarraylist

提问by user1796222

I have to develop one Android application. Here I am getting following error:

我必须开发一个 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(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.access0(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)

Here I have used below code:

在这里我使用了以下代码:

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++;
}           

If the data is empty, it means I have to display the "No Records" message. Otherwise display the correct data.

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

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);

Here I have wrote the code for checking the empty data.

这里我写了检查空数据的代码。

What is causing this exception?

是什么导致了这个异常?

回答by Sam

IndexOutOfBoundsException: Invalid index 1, size is 1

how can i resolve these error ???

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

You need to check the size of your array, the problem seems to happen with: Appscontent.Sub_arraylistvideosub.get(j)Simply check if your index is valid before trying to use it, like:

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

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