BitmapFactory.decodeResources возвращает нулевой объект
Я пытаюсь нарисовать значок под строкой RecyclerView при его перелистывании, аналогично приложению Gmail или Inbox. Я реализовал цвет, но растровое изображение, которое я пытаюсь нарисовать, уклоняется от меня. Я использую значок удаления с веб-сайта https://material.io/icons/. Я загрузил его в формате SVG и импортировал в папку Drawables в виде файла XML. Мой код выглядит следующим образом:
private Paint p = new Paint();
private ItemTouchHelper.SimpleCallback ithSC = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
fileData.remove(viewHolder.getAdapterPosition());
adapter.notifyDataSetChanged();
}
@Override
public void onChildDraw(Canvas c, RecyclerView rv, RecyclerView.ViewHolder vh, float dx, float dy, int actionState, boolean isCurrentlyActive) {
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.ic_delete);
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
// this gets the RecyclerView row from the ViewHolder
View itemView = vh.itemView;
// p is a Paint object, setting the color
p.setColor(Color.RED);
c.drawRect(dx, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom(), p);
super.onChildDraw(c, rv, vh, dx,dy, actionState, isCurrentlyActive);
}
}
};
Когда я запускаю приложение, у logcat появляется сообщение:
D/skia: ---- fAsset->read(8192) returned 0
D/skia: --- SkAndroidCodec::NewFromStream returned null
Почему вызов BitmapFactory.decodeResource() возвращает нулевой объект и как я могу это исправить?
заранее спасибо
ОБНОВИТЬ:
Я пробовал несколько других вещей, вот мой обновленный код. Одно из больших изменений заключается в том, что значок - это PNG, а не XML.
public void onChildDraw(Canvas c, RecyclerView rv, RecyclerView.ViewHolder vh, float dx, float dy, int actionState, boolean isCurrentlyActive) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
// this gets the RecyclerView row from the ViewHolder
View itemView = vh.itemView;
// get the width and height
float height = itemView.getHeight();
float width = height/3;
// p is a Paint object, setting the color
p.setColor(Color.RED);
c.drawRect(dx, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom(), p);
Bitmap b = BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.ic_delete);
RectF destination = new RectF((float) itemView.getRight() - 2*width , (float) itemView.getTop() + width, (float) itemView.getRight() - width, (float)itemView.getBottom() - width);
c.drawBitmap(b, null, destination, p);
super.onChildDraw(c, rv, vh, dx,dy, actionState, isCurrentlyActive);
}
}
};
Вот ошибка, которую я все еще получаю:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference