A
A
Alexander Kolmachikhin2018-10-27 16:35:49
Java
Alexander Kolmachikhin, 2018-10-27 16:35:49

Why am I getting null after BitmapFactory.decodeResource(getResources(), R.drawable.ic_shild)?

My Code:
MainActivity

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new DrawView(this));
    }

DrawView
public class DrawView extends View {
    // Здесь я получаю null
    private Bitmap img  = BitmapFactory.decodeResource(getResources(), R.drawable.ic_shild);

    public DrawView(Context context){
        super(context);
    }

    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);

        canvas.drawBitmap(img, 100, 100, null);
    }
}

My problem is that I am getting null instead of a picture on the screen. How to fix it?
Here is the full text, errors:
10-27 22:22:15.536 2863-2863/k.alexander.canvas_learn E/AndroidRuntime: FATAL EXCEPTION: main
    Process: k.alexander.canvas_learn, PID: 2863
    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:579)
        at k.alexander.canvas_learn.DrawView.onDraw(DrawView.java:22)
        at android.view.View.draw(View.java:15239)
        at android.view.View.updateDisplayListIfDirty(View.java:14175)
        at android.view.View.getDisplayList(View.java:14197)
        at android.view.View.draw(View.java:14967)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3410)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3203)
        at android.view.View.updateDisplayListIfDirty(View.java:14170)
        at android.view.View.getDisplayList(View.java:14197)
        at android.view.View.draw(View.java:14967)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3410)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3203)
        at android.view.View.updateDisplayListIfDirty(View.java:14170)
        at android.view.View.getDisplayList(View.java:14197)
        at android.view.View.draw(View.java:14967)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3410)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3203)
        at android.view.View.updateDisplayListIfDirty(View.java:14170)
        at android.view.View.getDisplayList(View.java:14197)
        at android.view.View.draw(View.java:14967)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3410)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3203)
        at android.view.View.updateDisplayListIfDirty(View.java:14170)
        at android.view.View.getDisplayList(View.java:14197)
        at android.view.View.draw(View.java:14967)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3410)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3203)
        at android.view.View.draw(View.java:15242)
        at android.widget.FrameLayout.draw(FrameLayout.java:598)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2658)
        at android.view.View.updateDisplayListIfDirty(View.java:14175)
        at android.view.View.getDisplayList(View.java:14197)
        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:2536)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2352)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1982)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5891)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5298)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

PS Tell me what else should be emphasized in the study, so as not to ask such stupid questions)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kolmachikhin, 2018-10-28
@Alexander_Kolmachikhin

I did not find the answer, but I found a solution: I tried to use my drawn vectors, and not the system ones that AS offers. Although why the AS vectors do not work is still not clear ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question