wasabeef / Blurry

Blurry is an easy blur library for Android
Apache License 2.0
5.57k stars 602 forks source link

NullPointerException: Attempt to invoke virtual method 'void android.graphics.Bitmap.recycle()' on a null object reference #50

Open ugurcany opened 7 years ago

ugurcany commented 7 years ago

I'm getting the following err:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Bitmap.recycle()' on a null object reference
       at jp.wasabeef.blurry.internal.Blur.of(SourceFile:42)
       at jp.wasabeef.blurry.Blurry$Composer.onto(SourceFile:115)
       ...................

...while calling the following:

Blurry.with(activity)
    .radius(40)
    .sampling(2)
    .color(ResourcesUtil.getColor(R.color.dirtyBlack25))
    .onto(blurredView);

As far as I understood, the err occurs here inside of method of Blur class:

Bitmap cache = view.getDrawingCache();
cache.recycle();

Here, the drawing cache can be null sometimes. This err occurs rarely. However, could you please solve this and update the library accordingly?

edwoollard commented 7 years ago

I can confirm I'm also receiving exactly the same issue with the same code sample.

HugoMatilla commented 7 years ago

Same here.

dominikmicuta commented 7 years ago

+1

zxx714 commented 7 years ago

@ugurcany When did you call Blurry.with? Is it too early? I mean before the view is attached to window?

However I think Blurry shouldn't make app crash or give some meaningful exception under this case at least.

ugurcany commented 7 years ago

@zxx714 In my case, the creation of blur view is done inside onShowListener() of a dialog that I create. And the dialog creation is triggered by a user input.

zxx714 commented 7 years ago

@ugurcany Did you use Blurry to blur the background of your dialog?

What's the blurredView you passed in your code?

ugurcany commented 7 years ago

@zxx714 Yes, we can say that. I'm passing the rootview of my Activity as the view to be blurred. Just want to mention again that I rarely encounter this issue.

kisinga commented 7 years ago

For anybody facing this problem, this is probably related to running the code too soon after the view is loaded try this attach a runnable imageView.post(new Runnable() { @Override public void run() { Blurry.with(Payment.this) .radius(25) .sampling(2) .animate(500) .onto(payments); } });

AlohaQwQ commented 7 years ago

I found the following code from the source code. public static Bitmap of(View view, BlurFactor factor) { view.setDrawingCacheEnabled(true); view.destroyDrawingCache(); view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW); Bitmap cache = view.getDrawingCache(); Bitmap bitmap = of(view.getContext(), cache, factor); cache.recycle(); return bitmap; } I don't know why call destroyDrawingCache() then call view.getDrawingCache(), so that may the cache is null,and then call cache.recycle(). This problem is caused by the difference version? And this is my calling code. Blurry.with(MainActivity.this) .capture(findViewById(R.id.coord_layout)) .into((ImageView) findViewById(R.id.bluur_img));

ameersabith commented 6 years ago

I have a problem while calling notifyDataSetChanged();.

Error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.sabith.restorentbiller.ItemsViewAdapter.notifyDataSetChanged()' on a null object reference

MensObscura commented 6 years ago

Hello, i tried to use it on a FrameLayout, Containing a GLSurfaceView Which is displaying the Camera Stream. First i got the '"NullPointerException: Attempt to invoke virtual method 'void android.graphics.Bitmap.recycle()'"

And then when I try using the pos(...)t method, it just does have no effect =) I 'assume this lib can't work on viewGroup with non-fixed background/ChildView =)

(Also related to this 'issue' https://github.com/wasabeef/Blurry/issues/73)

snowf07 commented 6 years ago

My solution,

handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            Blurry.with(mContext).radius(25).capture(mImgAudio).into(mImgBlurryfloat);

        }
    },1000);
jasonrobinson-gpsw commented 5 years ago

Kotlin approach:

private fun Blurry.Composer.postOnto(view: ViewGroup) {
    view.post { onto(view) }
}
sosmallsotiny commented 2 years ago

I faced same problem and tried all of above solutions but none of them works :( Also since "view.getDrawingCache()"has become decrypt since android API30, it seems necessary to delete all of the cache process...

Oshuremy commented 1 year ago

I'm facing this problem event with kotlin approach:

private fun Blurry.Composer.postOnto(view: ViewGroup) {
    view.post { onto(view) }
}