检查条件时数组索引超出范围异常

时间:2013-11-12 04:08:09

标签: java android arrays

我开始在android(传统的日本游戏)制作一款名为GO的游戏。 refferer:GO

并且我想让每个玩家都有一个转弯的条件,所以当玩家转弯时,条件会看到石头的位置,如果石头位置可以获得自由,那么运行条件,否则不是。这是编码:

public void checkCondition(int position, View v){

    final ImageButton[] arrPlayer1 = { //Board
            g1  ,g2,  g3,  g4,  g5,  g6,  g7,  g8,  g9, 
            g10, g11, g12, g13, g14, g15, g16, g17, g18, 
            g19, g20, g21, g22, g23, g24, g25, g26, g27, 
            g28, g29, g30, g31, g32, g33, g34, g35, g36, 
            g37, g38, g39, g40, g41, g42, g43, g44, g45, 
            g46, g47, g48, g49, g50, g51, g52, g53, g54, 
            g55, g56, g57, g58, g59, g60, g61, g62, g63, 
            g64, g65, g66, g67, g68, g69, g70, g71, g72, 
            g73, g74, g75, g76, g77, g78, g79, g80, g81};

    int posT = position - 9; //Top Position
    int posD = position + 9;  //Down Position
    int posL = position - 1; //Left Position
    int posR = position + 1; //Right Position

    ImageView BlackStone = (ImageView) findViewById(R.drawable.BlackStone);

    if(v == arrPlayer1[position] && arrPlayer1[posT] != null){
        if(arrPlayer1[posT] == BlackStone){
            if(v == arrPlayer1[position] && arrPlayer1[posD] != null){
                if(arrPlayer1[posD] == BlackStone){
                    if(v == arrPlayer1[position] && arrPlayer1[posR] != null){
                        if(arrPlayer1[posR] == BlackStone){
                            if(v == arrPlayer1[position] && arrPlayer1[posL] != null){
                                if(arrPlayer1[posL] == BlackStone){
                                    ChangeTurn(v);
                                }else{
                                    CheckLiberty(position, v);
                                }
                            }
                        }else{
                            CheckLiberty(position, v);
                        }
                    }
                }else{
                    CheckLiberty(position, v);
                }
            }
        }else{
            CheckLiberty(position, v);
        }
    }

} 

我的逻辑是,我在这个编码中创造了一个动态条件,所以无论玩家转向哪里,它都会看到石头的状态,例如:如果玩家在G20处放置石头,那么逻辑将在G11,G19中看到, G21,G29等相互连接的石头。

int posT = position - 9; //Top Position
int posD = position + 9;  //Down Position
int posL = position - 1; //Left Position
int posR = position + 1; //Right Position

但它仍然是错误的,并且logcat在方法(CheckCondition)中给出了错误数组超出范围的异常。所以我该怎么做 ?任何帮助表示赞赏

1 个答案:

答案 0 :(得分:2)

第一次改变:

ImageView BlackStone = (ImageView) findViewById(R.drawable.BlackStone);

ImageView BlackStone = (ImageView) findViewById(R.id.IdOfImageview);

您需要在使用Id时找到background而不是findViewById的任何组件的视图。