我正在为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构造函数。我不知道,为什么在对象创建时它会在引用这个数组的元素时抛出一个空指针异常......
答案 0 :(得分:3)
由于您编写了initializer
块并使用了无效的board。
public int[] pieces = new int [64];
//你在block
这里开始{。{1}}尚未初始化。
board
你要做的是创建一个名为intializer的方法并在那里
{
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 int[] pieces = new int [64];
private void intialize(){
{
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;
}
}
}
}
答案 1 :(得分: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乘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实例上调用它。