ImageView返回由Android中的Drawable文件设置的Null [重复]
Posted
技术标签:
【中文标题】ImageView返回由Android中的Drawable文件设置的Null [重复]【英文标题】:ImageView returns to Null set by the Drawable files in Android [duplicate] 【发布时间】:2021-11-14 15:26:06 【问题描述】:我知道这是一个常见问题,但我有一个问题,ImageView
返回 null。我只想知道是否可以从drawable中调用一个xml文件并将其转换为Byte []
。一件事它返回一个空值,因为我没有声明ImvTempCashCard = findViewById(R.id.ImageView);
。有没有其他方法可以在不使用ImageView的情况下调用可绘制图像?我只想将临时值设置为ImvTempCashCard
,它从可绘制文件中分配图像,它有必要用setBackgroundResource
到ImageView
吗?
错误
Attempt to invoke virtual method 'void android.widget.ImageView.setBackgroundResource(int)' on a null object reference
来源
ImageView ImvTempCashCard;
ImvTempCashCard.setBackgroundResource(R.drawable.ic_creditcard);
Bitmap bitmap = ((BitmapDrawable)ImvTempCashCard.getDrawable()).getBitmap();
ImvTempCashCard.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 120, 120, false));
ByteArrayOutputStream stream = new ByteArrayOutputStream();
byte [] byteimage = stream.toByteArray();
//byteimage = returns to null
最新更新:
实际上这将查看并添加到我的GridView
,在循环查询时,条件是,当 blob 图像为空时,它将替换可绘制文件图像。我正确实施了吗?
try
Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT id,cash_card_actual_no,hh_number,series_number,cc_image, id_image, cash_card_scanned_no FROM CgList");
list.clear();
while (cursor.moveToNext())
int id = cursor.getInt(0);
byte[] CashCardImage = cursor.getBlob(4);
if (CashCardImage.length ==1)
Bitmap bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.ic_creditcard);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
byte [] z = stream.toByteArray();
CashCardImage = z;
list.add(new Inventory(cashCardNumber, CashCardImage,id));
adapter.notifyDataSetChanged();
onCreate 内的适配器
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inventory_list);
gridView = (GridView) findViewById(R.id.gridView);
list = new ArrayList<>();
adapter = new InventoryListAdapter(this, R.layout.activity_inventory_items, list);
gridView.setAdapter(adapter);
InventoryListAdapter 类
public class InventoryListAdapter extends BaseAdapter
private Context context;
private int layout;
private ArrayList<Inventory> foodsList;
public InventoryListAdapter(Context context, int layout, ArrayList<Inventory> foodsList)
this.context = context;
this.layout = layout;
this.foodsList = foodsList;
@Override
public int getCount()
return foodsList.size();
@Override
public Object getItem(int position)
return foodsList.get(position);
@Override
public long getItemId(int position)
return position;
private class ViewHolder
ImageView imageView;
@Override
public View getView(int position, View view, ViewGroup viewGroup)
View row = view;
ViewHolder holder = new ViewHolder();
if(row == null)
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layout, null);
holder.imageView = (ImageView) row.findViewById(R.id.imgFood);
row.setTag(holder);
else
holder = (ViewHolder) row.getTag();
Inventory inventory = foodsList.get(position);
byte[] foodImage = inventory.getImage();
Bitmap bitmap = BitmapFactory.decodeByteArray(foodImage, 0, foodImage.length);
holder.imageView.setImageBitmap(bitmap);
return row;
【问题讨论】:
提示:尝试搜索您的根本问题的答案以避免XY problem @RickyMo 感谢您的回复,但为什么我得到一个空值,因为这条线存在于我的 drawable 中?ImvTempCashCard.setBackgroundResource(R.drawable.ic_creditcard);
否则,如果该行声明错误,我该如何声明可绘制文件?这样我就可以将其转换为位图
你自己回答了One thing it returns a null because I didn't declare ImvTempCashCard = findViewById(R.id.ImageView);
@Ricky Mo 是的,有没有其他方法可以在不使用 ImageView 的情况下调用可绘制图像?
查看我第一条评论中的链接。
【参考方案1】:
当从数据库中找不到图像时,您的目标是在网格视图中显示可绘制的占位符图像。您可以将所有逻辑放在getView()
中的adapater
中。检查图像数据是否不存在,然后调用setImageResource
而不是setImageBitmap
。
首先,构建列表时不需要做任何额外的步骤
Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT id,cash_card_actual_no,hh_number,series_number,cc_image, id_image, cash_card_scanned_no FROM CgList");
list.clear();
while (cursor.moveToNext())
int id = cursor.getInt(0);
byte[] CashCardImage = cursor.getBlob(4);
list.add(new Inventory(cashCardNumber, CashCardImage,id));
adapter.notifyDataSetChanged();
然后像这样更改您的getView()
:
@Override
public View getView(int position, View view, ViewGroup viewGroup)
View row = view;
ViewHolder holder = new ViewHolder();
if(row == null)
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layout, null);
holder.imageView = (ImageView) row.findViewById(R.id.imgFood);
row.setTag(holder);
else
holder = (ViewHolder) row.getTag();
Inventory inventory = foodsList.get(position);
byte[] foodImage = inventory.getImage();
if(foodImage.length > 1)
Bitmap bitmap = BitmapFactory.decodeByteArray(foodImage, 0, foodImage.length);
holder.imageView.setImageBitmap(bitmap);
else
holder.imageView.setImageResource (R.drawable.ic_creditcard)
return row;
提示:每当您想向视图显示不需要额外处理的内容时,请尽量直接执行。避免提取数据,将数据存储在某处并传递它等。只需将数据获取到您要直接显示的视图即可。
【讨论】:
我有一个错误,drawable 无法解析ImageView
中的方法setDrawable
image 我应该将其转换为位图吗?谢谢你知道我将如何写下来。
@Henry 对不起。方法名应该是setImageResource
而不是setDrawable
。我编辑了。 setImageResource
感谢您的回复我很感激,但我最近尝试过但我收到此错误? Image 因为我的drawable 是xml? Require type = Drawable provide = int
@Henry 使用setImageResource
而不是setImageDrawable
。
非常感谢!它解决了我的问题,感谢您的努力以上是关于ImageView返回由Android中的Drawable文件设置的Null [重复]的主要内容,如果未能解决你的问题,请参考以下文章
在 customView 中的 ImageView 上叠加许多图像视图