0

Android アプリケーションのグリッド ビュー用のカスタム アダプターに取り組んでいます。次のように定義されています。

public class BoardAdapter  extends BaseAdapter {

public static final int EMPTY = 0;
public static final int RED = 1;
public static final int BLACK = 2;
public static final int RED_PROMOTED = 3;
public static final int BLACK_PROMOTED = 4;


Context context;
int[][] board = null;


public int[] pieces = new int [64];
{
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {
    //null pointer exception here           
                if(board[i][j] == RED)
                {
                    pieces[8*i+j] = R.drawable.red_piece;
                }
                else if(board[i][j] == BLACK)
                {
                    pieces[8*i+j] = R.drawable.black_piece;
                }
                else pieces[8*i+j] = 0;
            }
        }
};


public BoardAdapter (Context ctx, int[][] board)
{
    this.context = ctx;
    this.board = board;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return pieces.length;
}

@Override
public Object getItem(int pos) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public long getItemId(int pos) {
    // TODO Auto-generated method stub
    return pieces[pos];
}

@Override
public View getView(int pos, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ImageView imageView = new ImageView(context);
    imageView.setImageResource(pieces[pos]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
    return imageView;       
}

}

アクティビティの onCreate メソッドでオブジェクトを作成します。

    protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);  
    board = (GridView) findViewById (R.id.board);
    game.printBoard();
    //null pointer exception here
    board.setAdapter(new BoardAdapter(this, game.getBoard()));

}

ボードを印刷すると、ログに正しい値が表示されます。したがって、初期化されたボードを BoardAdapter コンストラクターに渡すことは間違いありません。オブジェクトの作成時に、この配列の要素を参照するときに null ポインター例外がスローされる理由はわかりません...

4

2 に答える 2

1

加工順に

...
int[][] board = null; // this.board value assigned as null

public int[] pieces = new int [64]; //defining value, doesn't matter now

//WARNING this is an instance initializer block! Gets to run before the code of the constructor...
{ 
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
//null pointer exception here           
            if(board[i][j] == RED) //access stuff that does not exist...
            {
                pieces[8*i+j] = R.drawable.red_piece;
            }
            else if(board[i][j] == BLACK)
            {
                pieces[8*i+j] = R.drawable.black_piece;
            }
            else pieces[8*i+j] = 0;
        }
    }
};

...

将来のどこかでコンストラクターが呼び出される

  ...
  this.board = board; //board is assigned a value
  ...

編集

I want to assign the values of a 2 dimensional array board into a one-dimensional array pieces, so that I can get the appropriate images for the grid elements

次に、静的初期化ブロックではなく、それを達成するためのメソッドを作成する必要があります (voard は常に 8 x 8 であり、null ではないと仮定します)。

public int[] getPieces()
{ 
    int[] pieces = new int[64];
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            if(board[i][j] == RED)
            {
                pieces[8*i+j] = R.drawable.red_piece;
            }
            else if(board[i][j] == BLACK)
            {
                pieces[8*i+j] = R.drawable.black_piece;
            }
            else pieces[8*i+j] = 0;
        }
    }
    return pieces;
};

いつでも、BoardAdapter インスタンスでこれを呼び出します。

于 2013-09-25T14:25:22.750 に答える