我将字符串值传递给此方法。但是我正在获取数组索引绑定的异常,请帮帮我。

public void separateCards(String cards)
{
    cardNames+=cards+"-";
    String[] parts = cardNames.split("-");
    String part1 = parts[0]; // 004
    String part2 = parts[1]; // 034556
    Toast.makeText(getActivity(), part1,Toast.LENGTH_SHORT).show();
}
}


我的logcat

  03-28 11:27:41.213: E/AndroidRuntime(22392): FATAL EXCEPTION: main
    03-28 11:27:41.213: E/AndroidRuntime(22392): java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
    03-28 11:27:41.213: E/AndroidRuntime(22392):    at com.compareCr.ListvCompare.separateCards(ListvCompare.java:760)
    03-28 11:27:41.213: E/AndroidRuntime(22392):    at com.compareCr.ListvCompare$2$1.onClick(ListvCompare.java:627)
    03-28 11:27:41.213: E/AndroidRuntime(22392):    at android.view.View.performClick(View.java:4261)
    03-28 11:27:41.213: E/AndroidRuntime(22392):    at android.view.View$PerformClick.run(View.java:17356)
    03-28 11:27:41.213: E/AndroidRuntime(22392):    at android.os.Handler.handleCallback(Handler.java:615)
    03-28 11:27:41.213: E/AndroidRuntime(22392):    at android.os.Handler.dispatchMessage(Handler.java:92)

最佳答案

您将"-"附加在字符串的末尾,因此在拆分时,您只会得到一个值。

因此,此String part2 = parts[1];将数组索引抛出异常。

you need to put "-" in between string.

关于java - 错误java.lang.ArrayIndexOutOfBoundsException:,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22705400/

10-11 00:16