java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.isRecycled()' on a null object reference

前端 未结 6 1429
迷失自我
迷失自我 2021-01-06 01:16

I am trying to implement swipe to delete in RecyclerView. Everything seems to be working fine except drawing a delete icon below the item that\'s being swiped.

Thi

相关标签:
6条回答
  • 2021-01-06 01:28

    I don't understand why it;s null since I've already assigned it a value.

    Yes. You did => null. The problem is elsewhere. See docs for decodeResource():

    Returns: The decoded bitmap, or null if the image could not be decoded.

    so you need to a) always check for that condition, b) check why exactly it happens with the data you try to decode.

    0 讨论(0)
  • 2021-01-06 01:33

    The error is because of the drawable might be a Vector.

    Drawable drawable = ContextCompat.getDrawable(StreamActivity.this,R.drawable.ic_close)
    Bitmap icon = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(icon);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    
    0 讨论(0)
  • 2021-01-06 01:33

    I switched from:

    android:layout_width="1095dp"
    android:layout_height="1200dp"
    

    To:

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    

    Then the error went away.

    • Inspired by seekingStillness's answer
    0 讨论(0)
  • 2021-01-06 01:35

    Trying to decode a vector drawable into Bitmap, gives such error. So try to use any image with format .png, .jpeg etc in case of decoding resource to Bitmap.

    0 讨论(0)
  • 2021-01-06 01:45

    My problem was that android added a vector drawable anydpi along with the multiple created image assets for the icon. I deleted the anydpi variant and it all works fine now.

    0 讨论(0)
  • 2021-01-06 01:53

    Answer is check if bitmap is null or reycled() before passing it to ImageView.

    if(resource!==null && !resource.isRecycled)
        imageView.setImageBitmap(resource)
    
    0 讨论(0)
提交回复
热议问题