ttcreator
ttcreator

Reputation: 31

NullPointerException when i want setColor in my ImageView

When I try to set color for my imageView it gives me NullPointerException error

I've already tried a bunch of solutions, but I just can't figure out what the problem is. In XML files, I have a background color and an outline color and I don't understand why null. The compiler complains about these lines:

colourImageView.setColor(((ColorDrawable) color.getDrawable()).getColor());

and this:

changeCurrentColor(currentColor);

and this:

addEvent();

My code ColoringActivity.java:

public class ColoringActivity extends AppCompatActivity implements View.OnClickListener {

PhotoViewAttacher photoViewAttacher;
TableLayout colorTable;
ColourImageView colourImageView;
ImageView currentColor, cColor1, cColor2, cColor3;
int colorId;
ImageButton buttonPickColor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_coloring);

    initViews();
    addEvent();
}

private void initViews() {

    colourImageView = (ColourImageView) findViewById(R.id.color_image_view);
    colorTable = (TableLayout) findViewById(R.id.colortable);
    cColor1 = (ImageView) findViewById(R.id.current_pen1);
    cColor2 = (ImageView) findViewById(R.id.current_pen2);
    cColor3 = (ImageView) findViewById(R.id.current_pen3);
    buttonPickColor = (ImageButton) findViewById(R.id.button_pick_color);

}

public void addEvent() {

    Bitmap icon = BitmapFactory.decodeResource(this.getResources(),
            R.drawable.my_image);
    photoViewAttacher = new PhotoViewAttacher(colourImageView, icon);

    currentColor = cColor1;
    cColor1.setOnClickListener(checkCurrentColor);
    cColor2.setOnClickListener(checkCurrentColor);
    cColor3.setOnClickListener(checkCurrentColor);
    changeCurrentColor(currentColor);

    buttonPickColor.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openColorPicker();
        }
    });
}

private void openColorPicker() {

    if (currentColor != null) {

        AmbilWarnaDialog ambilWarnaDialog = new AmbilWarnaDialog(this, colorId,
                new AmbilWarnaDialog.OnAmbilWarnaListener() {
                    @Override
                    public void onCancel(AmbilWarnaDialog dialog) {
                    }

                    @Override
                    public void onOk(AmbilWarnaDialog dialog, int color) {
                        colorId = color;
                        changeCurrentColor(color);
                    }
                });
        ambilWarnaDialog.show();
    } else {
        Toast.makeText(this, "Please, set color", Toast.LENGTH_LONG).show();
    }

};

@Override
public void onClick(View v) {

}

private void setDefaultBg(int view1, int view2) {
    findViewById(view1).setBackgroundResource(R.drawable.set_color_default);
    findViewById(view2).setBackgroundResource(R.drawable.set_color_default);
}

View.OnClickListener checkCurrentColor = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.current_pen1:
                v.setBackgroundResource(R.drawable.set_color_setected);
                setDefaultBg(R.id.current_pen2, R.id.current_pen3);
                currentColor = (ImageView) v;
                changeCurrentColor((ImageView) v);
                break;
            case R.id.current_pen2:
                v.setBackgroundResource(R.drawable.set_color_setected);
                setDefaultBg(R.id.current_pen1, R.id.current_pen3);
                currentColor = ((ImageView) v);
                changeCurrentColor((ImageView) v);
                break;
            case R.id.current_pen3:
                v.setBackgroundResource(R.drawable.set_color_setected);
                setDefaultBg(R.id.current_pen1, R.id.current_pen2);
                currentColor = ((ImageView) v);
                changeCurrentColor((ImageView) v);
                break;
        }
    }
};

public void changeCurrentColor(ImageView color) {
        colourImageView.setColor(((ColorDrawable) color.getDrawable()).getColor());

}

private void changeCurrentColor(int color) {
    colourImageView.setColor(color);
    currentColor.setImageDrawable(new ColorDrawable(color));
}

Code my activity_coloring.xml:

<RelativeLayout 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"
    tools:context=".ColoringActivity">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/bottomlay"
        android:layout_width="match_parent"
        android:layout_height="38dp"
        android:layout_alignParentBottom="true"
        android:background="@drawable/maincolor_border"
        android:orientation="horizontal">

        <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:padding="2dp">

            <androidx.appcompat.widget.LinearLayoutCompat
                android:id="@+id/radio_group"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/current_pen1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="3dp"
                    android:layout_weight="1"
                    android:background="@drawable/set_color_setected"
                    android:padding="2dp" />

                <ImageView
                    android:id="@+id/current_pen2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="3dp"
                    android:layout_weight="1"
                    android:background="@drawable/set_color_default"
                    android:padding="2dp" />

                <ImageView
                    android:id="@+id/current_pen3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="3dp"
                    android:layout_weight="1"
                    android:background="@drawable/set_color_default"
                    android:padding="2dp" />
            </androidx.appcompat.widget.LinearLayoutCompat>

        <TableLayout
            android:id="@+id/colortable"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3">

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

                <androidx.appcompat.widget.AppCompatButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#ffbe0472" />

                <androidx.appcompat.widget.AppCompatButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#ffed008c" />

                <androidx.appcompat.widget.AppCompatButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#fff94cb2" />

                <androidx.appcompat.widget.AppCompatButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#ff0154a4" />

                <androidx.appcompat.widget.AppCompatButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#ff0083cb" />

                <androidx.appcompat.widget.AppCompatButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#ff19c1f2" />

                <androidx.appcompat.widget.AppCompatButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#ff4cb848" />

                <androidx.appcompat.widget.AppCompatButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#ffa3f345" />

                <androidx.appcompat.widget.AppCompatButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#ffc4d72d" />

                <androidx.appcompat.widget.AppCompatButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#ffff7e00" />
            </TableRow>
        </TableLayout>
    </androidx.appcompat.widget.LinearLayoutCompat>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottomlay"
        android:layout_below="@+id/tools_pain_top_bar">

    <uk.co.senab.photoview.ColourImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/color_image_view"
        android:src="@drawable/my_image">
    </uk.co.senab.photoview.ColourImageView>
    </FrameLayout>
    <include layout="@layout/tools_paint_top_bar"
        android:id="@+id/tools_pain_top_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">

    </include>
</RelativeLayout>

Code set_color_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true"
            android:drawable="@drawable/set_color_setected"
            />
    <item android:state_checked="false"
        android:drawable="@drawable/set_color_default"
        />
</selector>

Code set_color_selected.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/darker_gray" />
    <stroke
        android:width="2dp"
        android:color="@color/sea_blue" />
</shape>

code set_default_color.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/white" />
    <stroke
        android:width="2dp"
        android:color="@color/light_grey" />
</shape>

In logs:

2022-07-12 18:42:27.515 11041-11041/com.example.mycoloring E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mycoloring, PID: 11041
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mycoloring/com.example.mycoloring.ColoringActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.drawable.ColorDrawable.getColor()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3685)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3842)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7842)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.drawable.ColorDrawable.getColor()' on a null object reference
    at com.example.mycoloring.ColoringActivity.changeCurrentColor(ColoringActivity.java:142)
    at com.example.mycoloring.ColoringActivity.addEvent(ColoringActivity.java:71)
    at com.example.mycoloring.ColoringActivity.onCreate(ColoringActivity.java:47)
    at android.app.Activity.performCreate(Activity.java:8054)
    at android.app.Activity.performCreate(Activity.java:8034)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1341)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3666)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3842) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loopOnce(Looper.java:201) 
    at android.os.Looper.loop(Looper.java:288) 
    at android.app.ActivityThread.main(ActivityThread.java:7842) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 

Upvotes: 0

Views: 221

Answers (2)

ttcreator
ttcreator

Reputation: 31

I found a solution,

I Just set background and src attributes in ImageView tag in activity_coloring.xml file:

android:src="@color/white"
android:background="@drawable/set_color_default"

Upvotes: 1

Gobu CSG
Gobu CSG

Reputation: 691

Update all the image views to src instead of background

android:src="@drawable/set_color_setected"

OR use getBackground method

  ColorFilter mColorFilter =  color.getBackground().getColor().getColorFilter()
YOUR_MODIFY_VIEW.setColorFilter(mColorFilter)

Upvotes: 0

Related Questions