Android尝试调用虚方法

时间:2017-07-10 15:46:03

标签: java android nullpointerexception

我根据照片类型使用2种布局制作列表视图。如果照片是正方形,则使用方形照片布局,否则使用矩形,以及其他属性。我试图解决问题,但我无法弄清楚为什么我得到NullPointerException,即使我已初始化ImageView。以下是我的代码:

OPTIONS

我收到错误的行分别被注释为错误1和错误2。以下是我的错误日志:

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

LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View row;
final CartPhoto cp = photos.get(position);

if (convertView == null) {
    if(cp.image.width != cp.image.height){  //  Check for not Square Photo
        row = inflater.inflate(layoutId, null);
    }
    else
        row = inflater.inflate(layoutId_square, null);  //  Square Photo

} else {
    row = convertView;
}

final ImageView iv = (ImageView) row.findViewById(R.id.sq_image);
final ImageView iv_sqr = (ImageView) row.findViewById(R.id.sq_image_square);

float finalAspect = Product.aspectForProductSize(Product.ProductSize.Size4x6);
Bitmap thumb = cp.image.getThumbnail(context);

if(thumb != null) {
    if (thumb.getWidth() < thumb.getHeight()) { // portrait
        thumb = ImageOperations.cropToAspect(thumb, finalAspect);
    } else {
        thumb = ImageOperations.cropToAspect(thumb, 1/finalAspect);
    }


    if(cp.image.width != cp.image.height){

        iv.setImageBitmap(thumb);           // ERROR 1
        iv.setOnClickListener(imageClicked);
    }
    else {

        iv_sqr.setImageBitmap(thumb);      // ERROR 2
        iv_sqr.setOnClickListener(imageClicked);
    }
}
else {
    //  Crashlytics Log
    //String msg = "Else - Thumbnail Width : " + cp.image.width + ", Thumbnail Height : " + cp.image.height + "\n URI: " + cp.image.fullUrl;
    //Crashlytics.log(msg);
}

/***    Square or Rectangular    ***/
    /*
    if(cp.image.width == cp.image.height) {     //  SQUARE

        setupSizeQuantityButton(row, R.id.sq_4x4_minus, R.id.sq_4x4_qty, Product.ProductSize.Size4x4, -1, cp);
        setupSizeQuantityButton(row, R.id.sq_4x4_plus, R.id.sq_4x4_qty, Product.ProductSize.Size4x4, 1, cp);
        setupQuantityLabel(row, R.id.sq_4x4_qty, Product.ProductSize.Size4x4, cp);

        setupSizeQuantityButton(row, R.id.sq_8x8_minus, R.id.sq_8x8_qty, Product.ProductSize.Size8x8, -1, cp);
        setupSizeQuantityButton(row, R.id.sq_8x8_plus, R.id.sq_8x8_qty, Product.ProductSize.Size8x8, 1, cp);
        setupQuantityLabel(row, R.id.sq_8x8_qty, Product.ProductSize.Size8x8, cp);
    }
    else {
        setupSizeQuantityButton(row, R.id.sq_4x6_minus, R.id.sq_4x6_qty, Product.ProductSize.Size4x6, -1, cp);
        setupSizeQuantityButton(row, R.id.sq_4x6_plus, R.id.sq_4x6_qty, Product.ProductSize.Size4x6, 1, cp);
        setupQuantityLabel(row, R.id.sq_4x6_qty, Product.ProductSize.Size4x6, cp);

        setupSizeQuantityButton(row, R.id.sq_5x7_minus, R.id.sq_5x7_qty, Product.ProductSize.Size5x7, -1, cp);
        setupSizeQuantityButton(row, R.id.sq_5x7_plus, R.id.sq_5x7_qty, Product.ProductSize.Size5x7, 1, cp);
        setupQuantityLabel(row, R.id.sq_5x7_qty, Product.ProductSize.Size5x7, cp);

        setupSizeQuantityButton(row, R.id.sq_8x10_minus, R.id.sq_8x10_qty, Product.ProductSize.Size8x10, -1, cp);
        setupSizeQuantityButton(row, R.id.sq_8x10_plus, R.id.sq_8x10_qty, Product.ProductSize.Size8x10, 1, cp);
        setupQuantityLabel(row, R.id.sq_8x10_qty, Product.ProductSize.Size8x10, cp);
    }

return row;
}

2 个答案:

答案 0 :(得分:0)

final ImageView iv = (ImageView) row.findViewById(R.id.sq_image);
final ImageView iv_sqr = (ImageView) row.findViewById(R.id.sq_image_square);

在这里,我认为你有两种不同的布局分别用于矩形和方形图像。现在检查每个布局是否只有一个图像视图ID( R.id.sq_image R.id.sq_image_square )。

答案 1 :(得分:0)

尝试将其移至相应的ifs。

if (convertView == null) {
    if(cp.image.width != cp.image.height){  //  Check for not Square Photo
        row = inflater.inflate(layoutId, null);
        ImageView iv = (ImageView) row.findViewById(R.id.sq_image);
    }
    else
        row = inflater.inflate(layoutId_square, null);  //  Square Photo
        ImageView iv_sqr = (ImageView) row.findViewById(R.id.sq_image_square);    
} else {
    row = convertView;
}

我会在LayoutInflater下面实例化ImageView,如下所示:

LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
ImageView iv;
ImageView iv_sqr;