当前位置: 首页 > 知识库问答 >
问题:

如何调试java.lang.ArrayIndexOutOfBoundsException: length=12;索引=-1 [重复]

湛博易
2023-03-14

我有一个< code > recycle view ,单击它将显示存储在< code >数据库中的多个图像路径。当单击第1行直到最后一行时,它会显示< code>TextView中的多幅图像路径。但我的问题是,当第0行被点击时,应用程序崩溃。

这是我的代码

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flip);
        txtLoc = (TextView) findViewById(R.id.samp);

        Intent goFlip = getIntent();
        Bundle bundle = goFlip.getExtras();
        String getLocation = bundle.getString("path");
        index =bundle.getInt("pos");

        db = new DatabaseHandler(this);
        dbList = new ArrayList<GettersSetters>();
        dbList = db.searchFromDB(getLocation);

        String locate = "";
        if(dbList.size() > 0 ){

            for(GettersSetters currentClass : dbList){
                locate +=dbList.get(index-1).getPath() + ",";
                Log.w("RESULT PATHS", locate);
            }

        }
        txtLoc.setText(locate);
    }

它会产生以下错误。

09-09 17:33:39.141 24147-24147/com.luminous.pick E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.luminous.pick/com.luminous.pick.FlipActivity}: java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
at java.util.ArrayList.get(ArrayList.java:306)
at com.luminous.pick.FlipActivity.onCreate(FlipActivity.java:38)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135) 
at android.app.ActivityThread.access$700(ActivityThread.java:140) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4921) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511)

共有2个答案

阎功
2023-03-14

您不必从索引中减去 1,因为如果索引等于 0,则得到 -1。

华章横
2023-03-14

全局声明索引

 int index=0;

并使用此方法而不是您的方法....

 for(GettersSetters currentClass : dbList){
                locate +=dbList.get(index++).getPath() + ",";
                Log.w("RESULT PATHS", locate);
            }
 类似资料:
  • 我在尝试读取csv文件时遇到问题,错误如下所示: 。我尝试过在堆栈溢出中搜索,错误与上面相同。 法典: MyCSVFile:

  • 我试图迭代元素,在这里我必须为每个元素获取文本体,但在打印第一个元素体之后,下一个元素体我将获得“java.lang.IndexOutOfBoundsException:Index:1,Size:1”。我知道这是非常简单的修复,但我无法修复它。请帮助我解决此问题。 在下面的代码中,当“String text=KpiText.get(i).getText();”第二次打印时出现“java.lang.

  • 我的df有列'country'和'country code'作为当前索引。如何删除此索引并创建只计算行数的新索引?我会留下它的样子。我想做的就是在国家旁边添加一个新的索引。多谢!

  • 我对Python代码的以下部分有问题: 具体错误如下: 我做了一些研究,似乎在运行脚本时可能需要命令行中的参数,但是我不确定要添加什么或者问题是什么!我也搜索了这个网站,但是我找到的所有解决方案要么没有评论,要么没有像我一样包含开放功能。 任何帮助都非常感谢。

  • 正如标题所说,我的问题是,当我编译时,我在线程“main”java.lang.IndexOutOfBoundsException:Index:1,Size:1中得到了Exception。这是问题产生的部分。 编辑。 我一直在网上寻找解决方案,所以我很确定问题是数组太小,但我仍然不确定如何解决它。我试图让代码的上半部分只从数组中取出数字并将它们放入一个新的数组中。 这是我调用的其他方法。 我已经坚持