簡體   English   中英

Android-構造函數變量中的Null指針異常

[英]Android - Null pointer exception in a constructor variable

我正在為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構造函數。 我不知道,為什么在創建對象時,當引用該數組的元素時會引發空指針異常...

由於您已編寫了一個initializer塊並使用了null的board。

public int[] pieces = new int [64];

//您從這里開始一個block注意,該board尚未初始化。

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

您要做的是創建一個稱為Intializer的方法並在其中執行

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

}

現在在構造函數中調用該方法。

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

按處理順序

...
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實例上調用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM