首页 > 解决方案 > 为什么我有这个错误?ArrayIndexOutOfBoundsException

问题描述

我正在创建一个安卓应用程序。当我在 Emulator、Samsung S8 Plus 和 General Mobile GM8 中进行测试时,我没有任何错误,但游戏机总是让我有些迷恋。

代码是:

int count = getItemCount() >= 30 ? 30 : getItemCount();
if (count == 0)
    return new long[] {0};

long[] result = new long[count];
for (int i = 0; i < count; i++)
    result[i] = users.get(i).getPk();
return result;

错误消息: ArrayIndexOutOfBoundsException

我怎么能解决它我真的不知道。

标签: javaandroidandroid-studio

解决方案


删除“=”符号以使其正常工作:

int count = getItemCount() > 30 ? 30 : getItemCount();
        if (count == 0)
            return new long[] {0};

        long[] result = new long[count];
        for (int i = 0; i < count; i++)
            result[i] = users.get(i).getPk();
        return result;

推荐阅读