【问题标题】:Check if image view not null检查图像视图是否不为空
【发布时间】:2018-04-28 19:29:56
【问题描述】:

在我的应用程序中,我通过相机或图库插入图像,然后将其转换为 byte[] 以保存在 sqlite 数据库中。 插入时,我检查图像视图是否不为空,这是错误

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()'

这是我的代码(插入到 sqlite)

private void xuLyLuuTatCa() {

    String rangbuoc = etxtname.getText().toString();

    Bitmap bitmap = ((BitmapDrawable)imghinh3.getDrawable()).getBitmap();
    if(rangbuoc != null && rangbuoc.length() > 0
            || etxtadd.getText().toString() != null && etxtadd.getText().toString().length() > 0
            || etxtprice.getText().toString() != null && etxtprice.getText().toString().length() > 0
            || etxtopen.getText().toString() != null && etxtopen.getText().toString().length() > 0
            || etxtrate.getText().toString() != null && etxtrate.getText().toString().length() > 0
            || etxtdanhgia.getText().toString() != null && etxtdanhgia.getText().toString().length() > 0
            || imghinh1.getDrawable() != null || imghinh2.getDrawable() != null || bitmap != null) {

        db = database.initDatabase(this,DATABASE_NAME);
        String name = etxtname.getText().toString();
        String add = etxtadd.getText().toString();
        String price = etxtprice.getText().toString();
        String gio = etxtopen.getText().toString();
        double danhgia = Double.parseDouble(etxtrate.getText().toString());
        String binhluan = etxtdanhgia.getText().toString();
        byte[] anh = ConverttoArrayByte(imghinh1);
        byte[] anh2 = ConverttoArrayByte(imghinh2);
        byte[] anh3 = ConverttoArrayByte(imghinh3);
        boolean thich = false;
        ContentValues values = new ContentValues();
        //values.put("IDQuan", id);
        values.put("TenQuan", name);
        values.put("DiaChi", add);
        values.put("Gia", price);
        values.put("GioMoCua", gio);
        values.put("DanhGia", danhgia);
        values.put("Review", binhluan);
        values.put("YeuThich", thich);
        values.put("HinhAnh", anh);
        values.put("Hinh1", anh2);
        values.put("Hinh2", anh3);
        values.put("ViDo", 0);
        values.put("KinhDo", 0);
        String mess = "";
        if(db.insert("Quan",null,values) == -1)
        {
            mess = "Lỗi";
        }
        else
            mess = "Thành công";
        Toast.makeText(this, mess, Toast.LENGTH_SHORT).show();
        db.close();
        finish();
    }
    else
    {
        Toast.makeText(this, "Mời bạn nhập vào mục còn trống", Toast.LENGTH_LONG).show();
    }
}

public byte[] ConverttoArrayByte(ImageView img)
{
    BitmapDrawable bitmapDrawable = (BitmapDrawable) img.getDrawable();
    Bitmap bitmap=bitmapDrawable.getBitmap();

    ByteArrayOutputStream stream=new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    return stream.toByteArray();
}

那么如何检查 imageview 不为空???提前致谢

【问题讨论】:

  • if (imghinh3 != null && imghinh3.getDrawable() != null) ?
  • 另外,这个崩溃发生在xuLyLuuTatCa()ConverttoArrayByte() 内吗?
  • 在 xuLyLuuTatCa() 上,我的代码中没有“if (imghinh3 != null && imghinh3.getDrawable() != null)”行
  • 不,第一个评论是一个建议。尝试在 if 语句中包装 Bitmap bitmap = ((BitmapDrawable)imghinh3.getDrawable()).getBitmap(); 并使用 else 处理错误。虽然我认为这比解决问题更能掩盖问题
  • 感谢您的建议

标签: android sqlite null imageview bitmapimage


【解决方案1】:

你可以像这样在你的方法中使用 try catch

public byte[] ConverttoArrayByte(ImageView img) {
    try {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) img.getDrawable();
        Bitmap bitmap = bitmapDrawable.getBitmap();

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        return stream.toByteArray();

    } catch (NullPointerException e) {
        // do something
    }

    return null;
}

【讨论】:

    猜你喜欢
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    相关资源
    最近更新 更多