-1

Ok, so I am new to Android development, and I have been self-teaching from a few days. I am following some tutorials on youtube, but I am stuck on one of them, namely this one:

Fragment Activity Communication: https://www.youtube.com/watch?v=2c0Iog5rfxo&t=820s

The tutorial is about changing the background color of the main activity from the radio button selection on the fragment. I did exactly what he did in the tutorial, but I had to use the onAttach method differently because he used the deprecated way in the video i.e onAttach(Activity activity). So I used onAttach(Context context) (See code below)

But I am getting an error when I choose the radio button, the app crashes and the error log says:

FATAL EXCEPTION: main Process: fragmentapplication2, PID: 4318 java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.name.fragmentapplication2.ColorFragment$OnColorChangeListener.colorChanged(java.lang.String)' on a null object reference at com.example.name.fragmentapplication2.ColorFragment$1.onCheckedChanged(ColorFragment.java:36)

From this I get to know that the instance of the interface that I made in my Fragment class is null, which is causing this exception. Because I am using its instance to call its function in OnCheckedChangeListener of the RadioGroup. Below is the fragment class:

package com.example.name.fragmentapplication2;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

public class ColorFragment extends Fragment{
RadioGroup colorGroup;
OnColorChangeListener onColorChangeListener;//interface instance initiated

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view;
    view=inflater.inflate(R.layout.color_frag_layout,container,false);
    colorGroup=(RadioGroup)view.findViewById(R.id.color_radio_group);
    colorGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
        @Override
        public  void onCheckedChanged(RadioGroup group, int CheckedId){
            switch (CheckedId)
            {
                case R.id.Red:
                    onColorChangeListener.colorChanged("#CF000F");
                    break;
                case R.id.black:
                    onColorChangeListener.colorChanged("#000000");
                    break;
                case R.id.gray:
                    onColorChangeListener.colorChanged("#6C7A89");
                    break;
            }
        }
    });
    return view;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    Activity activity;
    if(context instanceof Activity)
    {
        activity = (Activity)context;
        try{
            onColorChangeListener = (OnColorChangeListener)activity;
        }catch (ClassCastException e){
            throw new ClassCastException(activity.toString()
                    + " must implement OnColorChangeListener");

        }
    }
}


public interface OnColorChangeListener
{
    public void colorChanged(String ColorName);
}

}

Logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.name.fragmentapplication2, PID: 4318
              java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.name.fragmentapplication2.ColorFragment$OnColorChangeListener.colorChanged(java.lang.String)' on a null object reference
                  at com.example.name.fragmentapplication2.ColorFragment$1.onCheckedChanged(ColorFragment.java:36)
                  at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
                  at android.widget.RadioGroup.access$600(RadioGroup.java:54)
                  at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:358)
                  at android.widget.CompoundButton.setChecked(CompoundButton.java:157)
                  at android.widget.CompoundButton.toggle(CompoundButton.java:113)
                  at android.widget.RadioButton.toggle(RadioButton.java:78)
                  at android.widget.CompoundButton.performClick(CompoundButton.java:118)
                  at android.view.View$PerformClick.run(View.java:19866)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5254)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

So how do I fix this problem? How do I initiate the instance of the interface so that it does not contain a null?

ZeusCodes
  • 21
  • 1
  • 8

1 Answers1

0

Remove the extra conditionals.

Activity activity;
if(context instanceof Activity) 
{
    activity = (Activity)context;

Just cast the Context to the listener. You shouldn't need to cast to an Activity first.

You'll get a null listener when context instanceof Activity is false.


If you really want an Activity, there should be onAttach(Activity activity) if you used Support Fragments class

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245