错误java.lang.IndexOutOfBoundsException:索引0无效,我的程序android中的大小为0

我开始在android中开发,我尝试修改Snake代码,包括调用浏览器来显示网页,当我执行我的应用程序时它显示在brwser中的页面,但问题是当我回到蛇应用程序时它显示应用程序意外停止的消息,logCat显示下面的例外情况

02-19 01:06:50.920: W/dalvikvm(3394): threadid=1: thread exiting with uncaught exception (group=0x4015a760) 02-19 01:06:50.930: E/AndroidRuntime(3394): FATAL EXCEPTION: main 02-19 01:06:50.930: E/AndroidRuntime(3394): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 02-19 01:06:50.930: E/AndroidRuntime(3394): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 02-19 01:06:50.930: E/AndroidRuntime(3394): at java.util.ArrayList.get(ArrayList.java:308) 02-19 01:06:50.930: E/AndroidRuntime(3394): at com.example.android.snake.SnakeView.updateSnake(SnakeView.java:448) 02-19 01:06:50.930: E/AndroidRuntime(3394): at com.example.android.snake.SnakeView.update(SnakeView.java:403) 02-19 01:06:50.930: E/AndroidRuntime(3394): at com.example.android.snake.SnakeView.setMode(SnakeView.java:335) 02-19 01:06:50.930: E/AndroidRuntime(3394): at com.example.android.snake.SnakeView.onKeyDown(SnakeView.java:278) 02-19 01:06:50.930: E/AndroidRuntime(3394): at android.view.KeyEvent.dispatch(KeyEvent.java:2387) 02-19 01:06:50.930: E/AndroidRuntime(3394): at android.view.View.dispatchKeyEvent(View.java:4569) 02-19 01:06:50.930: E/AndroidRuntime(3394): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1113) 02-19 01:06:50.930: E/AndroidRuntime(3394): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1113) 02-19 01:06:50.930: E/AndroidRuntime(3394): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1113) 02-19 01:06:50.930: E/AndroidRuntime(3394): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1113) 02-19 01:06:50.930: E/AndroidRuntime(3394): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1692) 02-19 01:06:50.930: E/AndroidRuntime(3394): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1260) 02-19 01:06:50.930: E/AndroidRuntime(3394): at android.app.Activity.dispatchKeyEvent(Activity.java:2231) 02-19 01:06:50.930: E/AndroidRuntime(3394): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1648) 02-19 01:06:50.930: E/AndroidRuntime(3394): at android.view.ViewRoot.deliverKeyEventPostIme(ViewRoot.java:2730) 02-19 01:06:50.930: E/AndroidRuntime(3394): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2703) 02-19 01:06:50.930: E/AndroidRuntime(3394): at android.view.ViewRoot.handleMessage(ViewRoot.java:1958) 02-19 01:06:50.930: E/AndroidRuntime(3394): at android.os.Handler.dispatchMessage(Handler.java:99) 02-19 01:06:50.930: E/AndroidRuntime(3394): at android.os.Looper.loop(Looper.java:126) 02-19 01:06:50.930: E/AndroidRuntime(3394): at android.app.ActivityThread.main(ActivityThread.java:3997) 02-19 01:06:50.930: E/AndroidRuntime(3394): at java.lang.reflect.Method.invokeNative(Native Method) 02-19 01:06:50.930: E/AndroidRuntime(3394): at java.lang.reflect.Method.invoke(Method.java:491) 02-19 01:06:50.930: E/AndroidRuntime(3394): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 02-19 01:06:50.930: E/AndroidRuntime(3394): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 02-19 01:06:50.930: E/AndroidRuntime(3394): at dalvik.system.NativeStart.main(Native Method) 02-19 01:08:07.450: D/A 

我觉得这很清楚

在com.example.android.snake.SnakeView.updateSnake(SnakeView.java:448)

您正在访问已实例化为new ArrayList(0) ,如果您至少读取错误,则非常简单。

ArrayList对象意味着自动resize,但如果您没有在零大小的数组列表中放置任何内容,则会出现此错误。

你有一个ArrayList其中包含0个元素。 在第448行的updateSnake()方法的SnakeView类中,您尝试访问索引0处的arraylist。索引0是您ArrayList的第一个元素,但如果它是空的,则索引0处没有元素。所以你得到一个IndexOutOfBoundsException