Java.lang.NullPointerException: attempt to call the interface method 'OnColorChangeListener.colorChanged (java.lang.String)' in a reference to a null object

I'm new to Android, so forgive me if this is a very simple solution. I reviewed other posts here regarding the same NullPointerException issue, however I still cannot find the source of the error in my code.

I have a very simple project with the main java class and fragment class. When the user clicks on the switch, the background color of the main action should change, but I keep getting:

java.lang.NullPointerException: attempt to call the interface method 'OnColorChangeListener.colorChanged (java.lang.String)' to reference a null object.

Activity5.java:

public class Activity5 extends AppCompatActivity implements ColorFragment.OnColorChangeListener {

    LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_5);
        linearLayout = (LinearLayout)findViewById(R.id.main_layout_id);
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        ColorFragment colorFragment = new ColorFragment();
        fragmentTransaction.add(R.id.fragment_container, colorFragment);
        fragmentTransaction.commit();
    }

    @Override
    public void colorChanged(String colorname) {
        if(colorname.equals("RED")) {
            linearLayout.setBackgroundColor(Color.RED);
        }
        else if(colorname.equals("GREEN")) {
            linearLayout.setBackgroundColor(Color.GREEN);
        }
        else if(colorname.equals("BLUE")){
            linearLayout.setBackgroundColor(Color.BLUE);
        }

        else if(colorname.equals("MAGENTA")) {
            linearLayout.setBackgroundColor(0xffff00ff);
        }
        else if(colorname.equals("YELLOW")) {
            linearLayout.setBackgroundColor(0xffffff00);
        }

    }
} 

Now here is the fragment class:

public class ColorFragment extends Fragment {

    OnColorChangeListener onColorChangeListener;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view;
        view = inflater.inflate(R.layout.color_fragment_layout, container, false);

        RadioGroup radioButtonGroup = (RadioGroup)view.findViewById(R.id.color_radio_group);
        radioButtonGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group, int checkIdOfButton) {

                switch (checkIdOfButton){
                    case R.id.red_id:
                        onColorChangeListener.colorChanged("RED");
                        break;
                    case R.id.green_id:
                        onColorChangeListener.colorChanged("GREEN");
                        break;
                    case R.id.blue_id:
                        onColorChangeListener.colorChanged("BLUE");
                        break;
                    case R.id.magenta_id:
                        onColorChangeListener.colorChanged("MAGENTA");
                        break;
                    case R.id.yellow_id:
                        onColorChangeListener.colorChanged("YELLOW");
                        break;
                }
            }
        });

        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {
            onColorChangeListener = (OnColorChangeListener) context;
        }
        catch catch (Exception e){}
        }
    }
    public interface OnColorChangeListener
    {
        public void colorChanged(String colorname);
    }
}

Here is the XML activity:

<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.user.seriousapp.Activity5"
    tools:showIn="@layout/activity_5"
    android:id="@+id/main_layout_id">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Fragment Communication Example"
        android:id="@+id/textView2"
        android:textColor="#000000"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/fragment_container"
        android:layout_marginTop="60dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:gravity="center"></RelativeLayout>

</LinearLayout>

, , XML-:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="250dp"
    android:background="#000000">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Select a Color"
        android:textColor="#ffffff"
        android:id="@+id/textView3"
        android:layout_gravity="center_horizontal" />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:orientation="vertical"
        android:id="@+id/color_radio_group">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RED"
            android:textColor="#ffffff"
            android:buttonTint="#ffffff"
            android:id="@+id/red_id" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="GREEN"
            android:textColor="#ffffff"
            android:buttonTint="#ffffff"
            android:id="@+id/green_id" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BLUE"
            android:textColor="#ffffff"
            android:buttonTint="#ffffff"
            android:id="@+id/blue_id" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="MAGENTA"
            android:textColor="#ffffff"
            android:buttonTint="#ffffff"
            android:id="@+id/magenta_id" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="YELLOW"
            android:textColor="#ffffff"
            android:buttonTint="#ffffff"
            android:id="@+id/yellow_id" />

    </RadioGroup>
</LinearLayout>
+4
2

, onAttach(Activity activity) , , Context, . , , , Android API 23+, ​​ API 23.

@Override
public void onAttach(Activity activity) {
    super.onAttach(context);

    try {
        onColorChangeListener = (OnColorChangeListener) context;
    }
    catch catch (Exception e){
        // you should really do something here with this exception
    }
}

, .

onAttach(Context context) API.

+5

onCreate() onActivityCreated(), getActivity()

onColorChangeListener = (OnColorChangeListener) getActivity();
+2

Source: https://habr.com/ru/post/1624714/


All Articles