我正在创建两个阵列列表,以阻止列表中的游戏:
ArrayList<Obstacle> TopWalls = null;
ArrayList<Obstacle> BottomWalls = null;
然后我在一个名为prep screen的方法中填充它们:
public void prepScreen(int width, int height){
ScreenH = height;
Num = (width)/center.getWidth()+4;
TopWalls = new ArrayList<Obstacle>();
BottomWalls = new ArrayList<Obstacle>();
for (int i=0;i<Num+1;i++){
Obstacle TO = new Obstacle(center, width+50+center.getWidth()*i,0);
TO.setManager (this);
TopWalls.add(TO);
Obstacle BO = new Obstacle(center, width+50+center.getWidth()*i,0);
BO.setManager (this);
TopWalls.add(BO);
}
GenerateObstacles();
}
然而,当我运行代码时,Log Cat告诉我有一个java.lang.IndexOutOfBoundsException
LOG CAT:
04-10 14:17:54.062: E/AndroidRuntime(1301): FATAL EXCEPTION: main
04-10 14:17:54.062: E/AndroidRuntime(1301): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.deepseadiver/com.example.deepseadiver.Game}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-10 14:17:54.062: E/AndroidRuntime(1301): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185)
04-10 14:17:54.062: E/AndroidRuntime(1301): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
04-10 14:17:54.062: E/AndroidRuntime(1301): at android.app.ActivityThread.access$600(ActivityThread.java:142)
04-10 14:17:54.062: E/AndroidRuntime(1301): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
04-10 14:17:54.062: E/AndroidRuntime(1301): at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 14:17:54.062: E/AndroidRuntime(1301): at android.os.Looper.loop(Looper.java:137)
04-10 14:17:54.062: E/AndroidRuntime(1301): at android.app.ActivityThread.main(ActivityThread.java:4931)
04-10 14:17:54.062: E/AndroidRuntime(1301): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 14:17:54.062: E/AndroidRuntime(1301): at java.lang.reflect.Method.invoke(Method.java:511)
04-10 14:17:54.062: E/AndroidRuntime(1301): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-10 14:17:54.062: E/AndroidRuntime(1301): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
04-10 14:17:54.062: E/AndroidRuntime(1301): at dalvik.system.NativeStart.main(Native Method)
04-10 14:17:54.062: E/AndroidRuntime(1301): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-10 14:17:54.062: E/AndroidRuntime(1301): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
04-10 14:17:54.062: E/AndroidRuntime(1301): at java.util.ArrayList.get(ArrayList.java:304)
04-10 14:17:54.062: E/AndroidRuntime(1301): at com.example.deepseadiver.Obstaclemanager.GenerateObstacles(Obstaclemanager.java:87)
04-10 14:17:54.062: E/AndroidRuntime(1301): at com.example.deepseadiver.Obstaclemanager.prepScreen(Obstaclemanager.java:62)
04-10 14:17:54.062: E/AndroidRuntime(1301): at com.example.deepseadiver.GamePanel.<init>(GamePanel.java:40)
04-10 14:17:54.062: E/AndroidRuntime(1301): at com.example.deepseadiver.Game.onCreate(Game.java:94)
04-10 14:17:54.062: E/AndroidRuntime(1301): at android.app.Activity.performCreate(Activity.java:5008)
04-10 14:17:54.062: E/AndroidRuntime(1301): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-10 14:17:54.062: E/AndroidRuntime(1301): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2139)
04-10 14:17:54.062: E/AndroidRuntime(1301): ... 11 more
然后,游戏面板类正在运行prepScreen方法:
OM.prepScreen(ScreenWidth, Screenheight);
Obstaclemanager.java:
package com.example.deepseadiver;
import java.util.ArrayList;
import android.graphics.Bitmap;
import android.graphics.Canvas;
public class Obstaclemanager {
Bitmap center;
//Variable for the height of the submarine
int subHeight;
int Num;
int ScreenH;
int dl;
int TargetY =-1;
int dpos;
public GamePanel gamePanel;
ArrayList<Obstacle> TopWalls = null;
ArrayList<Obstacle> BottomWalls = null;
public Obstaclemanager(Bitmap decodeResource, GamePanel gamePanel) {
//set center variable to the value passed into the method
center = decodeResource;
//set gamePanel variable to the value passed into the method
this.gamePanel = gamePanel;
}
//Method gets the height of the submarine
void setSubH(int h){
subHeight = h;
}
//Method to calculate how many obstacles to draw on screen and create them
public void prepScreen(int width, int height){
//Sets variable to the value passed into method
ScreenH = height;
//Sets the Num variable based on the width of the Background image 4 is added for good measure
Num = (width)/center.getWidth()+4;
//Sets the top and bottom walls to be an array of obstacles
TopWalls = new ArrayList<Obstacle>();
BottomWalls = new ArrayList<Obstacle>();
//For loop from 0 to the Num value
for (int i=0;i<Num+1;i++){
//TO (Top Obstacles) are created
Obstacle TO = new Obstacle(center, width+50+center.getWidth()*i,0);
//Obstacles are set
TO.setManager (this);
//Obstacles are added to the TopWalls array
TopWalls.add(TO);
//BO (Bottom Obstacles) are created
Obstacle BO = new Obstacle(center, width+50+center.getWidth()*i,0);
//Obstacles are set
BO.setManager (this);
//Obstacles are added to the BottomWalls array
TopWalls.add(BO);
}
GenerateObstacles();
}
//Method to generate the obstacles
private void GenerateObstacles() {
//h variable is set from the value of half the background bitmap
int h = center.getHeight()/2;
//sets the dl varible to the screen height variable
dl = ScreenH;
//sets the dpos variable to half the screen height so the center horozontal portion of the screen
dpos = ScreenH/2;
//sets a new dl variable whose values is 3/5 ths of the screen for when obstacles are placed
int new_dl = ScreenH*3/5;
//inc is set based on the screen height minus by the new_dl varable this is then divided by the Num variable which holds the number of obstacles
int inc = (dl-new_dl)/Num;
//For loop from 0 to the Num value
for (int i=0;i<Num+1;i++){
//dl is set on its original value minus the value of inc
dl = dl - inc;
//h is set to half the height of an obstacle
h = TopWalls.get(i).getBitmap().getHeight()/2;
//TopWalls obstacles are positioned
TopWalls.get(i).setY(dpos -dl/2-h);
//BottomWalls obstacles are psitioned
BottomWalls.get(i).setY(dpos +dl/2+h);
}
}
//Method to draw to canvas
public void draw(Canvas canvas){
//For loop from 0 to the Num value
for (int i=0;i<Num+1;i++){
//TopWalls are drawn to canvas
TopWalls.get(i).draw(canvas);
//BottomWalls are drawn to canvas
BottomWalls.get(i).draw(canvas);
}
}
//Method to update the canvas
public void update(float dt){
//For loop from 0 to the Num value
for (int i=0;i<Num+1;i++){
//TopWalls are updated
TopWalls.get(i).update(dt, true);
//BottomWalls are updated
BottomWalls.get(i).update(dt, false);
}
}
}
老实说,我已经看了大约3个小时了,无法弄清楚为什么会发生任何帮助或建议非常感谢。
答案 0 :(得分:2)
您的问题就在这一行for (int i=0;i<Num+1;i++){
当只有0
元素时,您将从Num + 1
转到Num