How to clear BitmapDrawable.getBitmap()' on a null object reference on a null object reference error

370 views Asked by At

Im using this library https://github.com/jgabrielfreitas/BlurImageView to blur images and everything works fine in a normal activity, recently i tried using this library in a custom GridView and got this error android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference This is my custom GridView adapter code to blur images gotten from a url with the help of Glide :

    image.setVisibility(View.INVISIBLE);
    Glide.with(getApplicationContext()).load(Uri.parse(lmap.get((int)_position).get("pic").toString())).into(image);
    t = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    image.setVisibility(View.VISIBLE);
                    image.setBlur(15);
                }
            });
        }
    };
    _timer.schedule(t, (int)(2000));
}

NOTE: image.setBlur(15); is a function from the library.

Here is my custom GridView layout xml :

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <androidx.cardview.widget.CardView
        android:id="@+id/cardview1"
        android:layout_width="200dp"
        android:layout_height="250dp"
        android:clickable="true"
        android:focusable="true"
        app:cardCornerRadius="10dp"
        app:cardElevation="2dp">
        <RelativeLayout
            android:id="@+id/linear1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <com.jgabrielfreitas.core.BlurImageView
                android:id="@+id/image"
                android:focusable="false"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                
                android:layout_weight="1"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                app:radius="24"/>
            <LinearLayout
                android:id="@+id/linear2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="vertical"
                android:layout_alignBottom="@+id/image">
                <LinearLayout
                    android:id="@+id/linear4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:id="@+id/name_age"
                        android:focusable="false"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="8dp"
                        android:text="Peter, 43"
                        android:textSize="16sp"
                        android:textColor="#FFFFFF"
                        android:translationY="10.0dp"
                        android:shadowColor="#000000"
                        android:shadowDx="1.5"
                        android:shadowDy="1.3"
                        android:shadowRadius="1.6"/>
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/linear6"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:orientation="horizontal"
                    android:translationY="-5.0dp">
                    <LinearLayout
                        android:id="@+id/linear3"
                        android:layout_width="10dp"
                        android:layout_height="10dp"
                        android:layout_marginLeft="10dp"
                        android:padding="8dp"
                        android:background="#C5E1A5"
                        android:orientation="vertical"/>
                    <TextView
                        android:id="@+id/time"
                        android:focusable="false"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="8dp"
                        android:text="Last seen recently"
                        android:textSize="14sp"
                        android:textColor="#FFFFFF"
                        android:shadowColor="#000000"
                        android:shadowDx="1.5"
                        android:shadowDy="1.3"
                        android:shadowRadius="1.6"/>
                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>
    </androidx.cardview.widget.CardView>
</LinearLayout>

NOTE: Since im getting the image from a url i have removed app:src="drawable/img"

1

There are 1 answers

0
PeterEssyen On

After spending almost a full day using different approach to solve the error, i finally found a way and it is simple as ABC i just needed to use a try catch statement on the image.setBlur(int) and it worked liked magic. A snippet from my GridView adapter would now look like this:

    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try{
                    image.setBlur(15);
                }catch(Exception e){
                     
                }
            }
        });
    }
};
_timer.schedule(t, (int)(2000)); ```