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

时间:2016-09-12 00:41:45

标签: java android bitmap nullpointerexception

我正在尝试在RecyclerView中实现滑动删除。除了在正在刷卡的项目下面绘制一个删除图标外,一切似乎都运行正常。

我就是这样做的:

ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                int position = viewHolder.getAdapterPosition();

//                Do my stuff
//                }
            }

            @Override
            public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {

                Bitmap icon;
                if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE){

                    View itemView = viewHolder.itemView;
                    float height = (float) itemView.getBottom() - (float) itemView.getTop();
                    float width = height / 3;

                    if(dX > 0){
                        p.setColor(Color.parseColor("#388E3C"));
                        RectF background = new RectF((float) itemView.getLeft(), (float) itemView.getTop(), dX,(float) itemView.getBottom());
                        c.drawRect(background,p);
                        icon = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.ic_delete);
                        RectF icon_dest = new RectF((float) itemView.getLeft() + width ,(float) itemView.getTop() + width,(float) itemView.getLeft()+ 2*width,(float)itemView.getBottom() - width);
                        c.drawBitmap(icon,null,icon_dest,p);
                    } else {
                        p.setColor(Color.parseColor("#D32F2F"));
                        RectF background = new RectF((float) itemView.getRight() + dX, (float) itemView.getTop(),(float) itemView.getRight(), (float) itemView.getBottom());
                        c.drawRect(background,p);
                        icon = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.ic_delete);
                        RectF icon_dest = new RectF((float) itemView.getRight() - 2*width ,(float) itemView.getTop() + width,(float) itemView.getRight() - width,(float)itemView.getBottom() - width);
                        c.drawBitmap(icon,null,icon_dest,p);
                    }
                }
                super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
            }
        };
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
        itemTouchHelper.attachToRecyclerView(mRecyclerView);

投掷

  

java.lang.NullPointerException:尝试调用虚方法   '对象上的'boolean android.graphics.Bitmap.isRecycled()'   参考

完整StackTrace

09-12 01:28:58.386 26575-26575/com.ozuf.booker E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.ozuf.booker, PID: 26575
                                                                     java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.isRecycled()' on a null object reference
                                                                         at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1281)
                                                                         at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:620)
                                                                         at com.ozuf.booker.fragments.BookFragment$1.onChildDraw(BookFragment.java:149)
                                                                         at android.support.v7.widget.helper.ItemTouchHelper$Callback.onDraw(ItemTouchHelper.java:1956)
                                                                         at android.support.v7.widget.helper.ItemTouchHelper$Callback.access$1400(ItemTouchHelper.java:1356)
                                                                         at android.support.v7.widget.helper.ItemTouchHelper.onDraw(ItemTouchHelper.java:542)
                                                                         at android.support.v7.widget.RecyclerView.onDraw(RecyclerView.java:3373)
                                                                         at android.view.View.draw(View.java:15635)
                                                                         at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:3308)
                                                                         at android.view.View.updateDisplayListIfDirty(View.java:14568)
                                                                         at android.view.View.getDisplayList(View.java:14590)
                                                                         at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3588)
                                                                         at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3567)
                                                                         at android.view.View.updateDisplayListIfDirty(View.java:14528)
                                                                         at android.view.View.getDisplayList(View.java:14590)
                                                                         at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3588)
                                                                         at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3567)
                                                                         at android.view.View.updateDisplayListIfDirty(View.java:14528)
                                                                         at android.view.View.getDisplayList(View.java:14590)
                                                                         at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3588)
                                                                         at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3567)
                                                                         at android.view.View.updateDisplayListIfDirty(View.java:14528)
                                                                         at android.view.View.getDisplayList(View.java:14590)
                                                                         at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3588)
                                                                         at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3567)
                                                                         at android.view.View.updateDisplayListIfDirty(View.java:14528)
                                                                         at android.view.View.getDisplayList(View.java:14590)
                                                                         at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3588)
                                                                         at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3567)
                                                                         at android.view.View.updateDisplayListIfDirty(View.java:14528)
                                                                         at android.view.View.getDisplayList(View.java:14590)
                                                                         at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3588)
                                                                         at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3567)
                                                                         at android.view.View.updateDisplayListIfDirty(View.java:14528)
                                                                         at android.view.View.getDisplayList(View.java:14590)
                                                                         at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3588)
                                                                         at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3567)
                                                                         at android.view.View.updateDisplayListIfDirty(View.java:14528)
                                                                         at android.view.View.getDisplayList(View.java:14590)
                                                                         at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3588)
                                                                         at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3567)
                                                                         at android.view.View.updateDisplayListIfDirty(View.java:14528)
                                                                         at android.view.View.getDisplayList(View.java:14590)
                                                                         at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3588)
                                                                         at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3567)
                                                                         at android.view.View.updateDisplayListIfDirty(View.java:14528)
                                                                         at android.view.View.getDisplayList(View.java:14590)
                                                                         at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273)
                                                                         at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279)
                                                                         at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318)
                                                                         at android.view.ViewRootImpl.draw(ViewRootImpl.java:2575)
                                                                         at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2385)
                                                                         at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2012)
                                                                         at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1073)
                                                                         at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5988)
                                                                         at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
                                                                        at android.view.Choreographer.doCallbacks(Chor

第149行是c.drawBitmap(icon,null,icon_dest,p);我已经调试了,我看到icon为空,但我不明白为什么; s null,因为我已经为它赋了一个值。

6 个答案:

答案 0 :(得分:11)

  

我不明白为什么;因为我已经给它赋了一个值,所以是null。

是。你做了=> null。问题出在其他地方。请参阅decodeResource()的文档:

  

返回:已解码的位图,如果图像无法解码,则为null

因此您需要a)始终检查该条件,b)检查您尝试解码的数据究竟发生的原因。

答案 1 :(得分:7)

错误是因为drawable可能是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);

答案 2 :(得分:1)

尝试将可绘制成矢量的矢量解码为Bitmap会出现此类错误。因此,在将资源解码为位图的情况下,请尝试使用任何格式为.png,.jpeg等的图片。

答案 3 :(得分:1)

在将位图传递给ImageView之前,先检查位图是 null 还是 reycled()

if(resource!==null && !resource.isRecycled)
    imageView.setImageBitmap(resource)

答案 4 :(得分:0)

我的问题是android向图标添加了矢量可绘制的anydpi以及多个创建的图像资产。我删除了anydpi变体,现在一切正常。

答案 5 :(得分:0)

我从:

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

收件人:

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

然后错误消失了。

  • 灵感来自寻求静ill的答案