Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 构造函数变量中的Android-空指针异常_Java_Android_Adapter - Fatal编程技术网

Java 构造函数变量中的Android-空指针异常

Java 构造函数变量中的Android-空指针异常,java,android,adapter,Java,Android,Adapter,我正在为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 fi

我正在为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;       
}

当我打印电路板时,日志显示正确的值。所以我确信,我将一个初始化的板传递给BoardAdapter构造函数。我不知道,为什么在对象创建时,它在引用此数组的元素时抛出空指针异常…

因为您编写了一个
初始值设定项
块,并且使用了一个为空的板

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 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;
            }
        }
};

按处理顺序

...
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;
        }
    }
};
编辑


我想将二维阵列板的值分配到一维阵列块中,以便为网格元素获得适当的图像

然后您应该创建一个方法来实现这一点,而不是一个静态初始值设定项块(假设voard始终是8乘8,并且不为null):

public int[]getPieces()
{ 
int[]件=新int[64];

对于(int i=0;i,按处理顺序

...
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;
        }
    }
};
编辑


我想将二维阵列板的值分配到一维阵列块中,以便为网格元素获得适当的图像

然后您应该创建一个方法来实现这一点,而不是一个静态初始值设定项块(假设voard始终是8乘8,并且不为null):

public int[]getPieces()
{ 
int[]件=新int[64];

对于(inti=0;iwhat是
game.printBoard();
npe=>post-stacktraceees,board是空的。这有什么奇怪的?什么是
game.printBoard()
npe=>post-stackTraceEyes,board为空。这有什么奇怪的?做什么?你想实现什么?你想从构造器中接收到的board元素填充pieces数组吗?我想把二维数组board的值分配到一维数组pieces中,这样我就可以得到适当的值网格元素的图像创建一个方法,然后在那里执行,并在初始化完成后调用该方法。PS不是一个安卓人。@ppeterka66我想初始化就足够了。不需要返回,如果我错了,请更正我。@MichałSzydłowski更新了答案,提供了一个获取1d数组的方法。做什么?你想做什么chieve?你想从构造器中接收到的电路板元素填充pieces数组吗?我想将二维阵列电路板的值分配到一维数组pieces中,这样我就可以获得网格元素的适当图像创建一个方法,并在初始化完成后调用该方法.PS不是安卓人。@ppeterka66我想初始化就足够了。如果我错了,无需返回,请纠正我。@MichałSzydłowski更新了答案,提供了获取1d数组的方法。@MichałSzydłowski很高兴它有帮助。ppeterka66还为您的问题提供了另一种可能的解决方案。请查看编辑并标记正确答案。@Micha很高兴这有帮助。佩特卡66还为你的问题提供了另一种可能的解决方案。请查看编辑并标记正确答案。
  ...
  this.board = board; //board is assigned a value
  ...
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;
};