Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable' on a null object reference

java.lang.RuntimeException: Unable to destroy activity
{com.example.demo.androidAppdemo/com.example.demo.androidAppdemo.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)' on a null object reference
        at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3945)
        at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3963)
        at android.app.ActivityThread.access$1700(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1454)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5604)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)' on a null object reference
        at com.example.demo.androidAppdemo.V2.NavigationDrawer.NavigationDrawerFragment$2.setResource(NavigationDrawerFragment.java:594)
        at com.example.demo.androidAppdemo.V2.NavigationDrawer.NavigationDrawerFragment$2.setResource(NavigationDrawerFragment.java:588)
        at com.bumptech.glide.request.target.ImageViewTarget.setResourceInternal(ImageViewTarget.java:127)
        at com.bumptech.glide.request.target.ImageViewTarget.onLoadCleared(ImageViewTarget.java:97)
        at com.bumptech.glide.request.SingleRequest.clear(SingleRequest.java:319)
        at com.bumptech.glide.manager.RequestTracker.clearRemoveAndMaybeRecycle(RequestTracker.java:79)
        at com.bumptech.glide.manager.RequestTracker.clearRemoveAndRecycle(RequestTracker.java:66)
        at com.bumptech.glide.RequestManager.untrack(RequestManager.java:603)
        at com.bumptech.glide.RequestManager.untrackOrDelegate(RequestManager.java:571)
        at com.bumptech.glide.RequestManager.clear(RequestManager.java:559)
        at com.bumptech.glide.RequestManager.onDestroy(RequestManager.java:303)
        at com.bumptech.glide.manager.ActivityFragmentLifecycle.onDestroy(ActivityFragmentLifecycle.java:65)
        at com.bumptech.glide.manager.SupportRequestManagerFragment.onDestroy(SupportRequestManagerFragment.java:194)
        at android.support.v4.app.Fragment.performDestroy(Fragment.java:2588)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1566)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
        at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3244)
        at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:3235)
        at android.support.v4.app.Fragment.performDestroy(Fragment.java:2583)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1566)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
        at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3244)
        at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:3235)
        at android.support.v4.app.FragmentController.dispatchDestroy(FragmentController.java:265)
        at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:390)
        at android.support.v7.app.AppCompatActivity.onDestroy(AppCompatActivity.java:209)
        at com.example.demo.androidAppdemo.BaseActivity.onDestroy(BaseActivity.java:867)
        at com.example.demo.androidAppdemo.MainActivity.onDestroy(MainActivity.java:783)
        at android.app.Activity.performDestroy(Activity.java:6529)
    at android.app.Instrumentation.callActivityOnDestroy(

Above error thrown while closing app-BaseActivity is being destroyed. Getting this error while setting the Profile Photo of user in the navigation drawer fragment. This error is thrown when closing the app just after app starts. This error is being thrown in the below code while setting image using setImageDrawable after the converting the image to circular image using the RoundedBitmapDrawableFactory and using Glide V4.

 GlideApp.with(NavigationDrawerFragment.this).asBitmap().load(profilePhotoUrl).centerCrop().into(new BitmapImageViewTarget(navigation_header_splash_icon) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
  RoundedBitmapDrawableFactory.create(NavigationDrawerFragment.this.getResources(),resource);
                        circularBitmapDrawable.setCircular(true);
//getting error in below line
                        navigation_header_splash_icon.setImageDrawable(circularBitmapDrawable);
                    }
                });

Replace the above
    GlideApp.with(NavigationDrawerFragment.this)
                        .asBitmap()
                        .load(profilePhotoUrl)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .error(R.drawable.splash_icon)
                        .placeholder(R.drawable.splash_icon)
                        .transforms(new RoundedCorner( Application.get(),dpToPx(75/2), 1))
                        .into(navigation_header_splash_icon);

or Use glide V 4 library RequestOptions to crop the image to circular image
Glide.with(NavigationDrawerFragment.this)
        .load(profilePhotoUrl)
        .apply(RequestOptions.circleCropTransform())
        .into(navigation_header_splash_icon);

No comments:

Post a Comment

Popular Posts