Android,图像适配器,getCount()中的空指针异常

时间:2014-10-18 11:13:42

标签: android gridview

我想使用一个整数来设置图像适配器添加到gridview的图像数量而不是数组的长度,但是当我在getCount()中放入其他任何内容时,它总是会给出空指针异常。我需要设置它,因为我随机地将图像添加到网格中,我希望网格中显示一定数量的图像,具体取决于从上一个活动解析的整数。我也尝试制作一些if语句来检查diff是否为2或3,如果是这样,它将返回其他一个数组的长度,并设置一个else语句,所以如果diff是任何其他数字,它将使用当前数组长度,但这也给了一个空指针。

那么如何让它为我的gridview添加动态数量的图像而不是一个数组的长度呢?

Integer diff, gridSize, randomPosition, item;
GridView grid;
ImageView img;
Random rng;
ImageView imageView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    grid = (GridView)findViewById(R.id.grid);
    grid.setAdapter(new ImageAdapter(this));
    rng = new Random();

    Bundle pack = this.getIntent().getExtras();
    diff = pack.getInt("dif");

    if (diff == 1){
        gridSize = 16;
        grid.setNumColumns(4);
    }

    if (diff == 2){
        gridSize = 25;
        grid.setNumColumns(5);
    }

    if (diff == 3){
        gridSize = 36;
        grid.setNumColumns(6);
    }


public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return gridSize; // this used to return mThumbIds.length by default and it worked then
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
            randomPosition = rng.nextInt(gridSize);
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(3, 3, 3, 3); 
            } 
            else {
                imageView = (ImageView) convertView;
            }

            imgSet();
            return imageView;
    }

    public void imgSet(){
        if (diff == 1){
            imageView.setImageResource(mThumbIds[randomPosition]);
        }
        if (diff == 2){
            imageView.setImageResource(mThumbIds2[randomPosition]);
        }
        if (diff == 3){
            imageView.setImageResource(mThumbIds3[randomPosition]);
        }
    }

然后我有了我的图像阵列。

2 个答案:

答案 0 :(得分:0)

在设置图像适配器之前执行此操作

if (diff == 1){
    gridSize = 16;
    grid.setNumColumns(4);
}

if (diff == 2){
    gridSize = 25;
    grid.setNumColumns(5);
}

if (diff == 3){
    gridSize = 36;
    grid.setNumColumns(6);
}

让我知道它是否无效

答案 1 :(得分:0)

在 之后设置适配器 获取' diff'的值和' rng',像这样:

grid = (GridView)findViewById(R.id.grid);
rng = new Random();

Bundle pack = this.getIntent().getExtras();
diff = pack.getInt("dif");

if (diff == 1){
    gridSize = 16;
    grid.setNumColumns(4);
}

if (diff == 2){
    gridSize = 25;
    grid.setNumColumns(5);
}

if (diff == 3){
    gridSize = 36;
    grid.setNumColumns(6);
}

grid.setAdapter(new ImageAdapter(this));