-2

I am having two fragments side by side in on the MainActivity LeftFragment has a RecyclerView with colors and I want to set the background color and text on the Right Fragment when tap on any color on the LeftFragment

The app is crashing with the error null object reference error.

Here is my Github Repo. However, adding some code for reference.

Left Fragment

public class LeftFragment extends Fragment {

    public LeftFragment() {
        // Required empty public constructor
    }

    public static LeftFragment newInstance() {
        return new LeftFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_left, container, false);

        //    private OnColorChooseListener mOnColorChooseListener;
        ArrayList<Color> mColor = new ArrayList<>();

        mColor.add(new Color(getResources().getColor(R.color.colorRed), getResources().getString(R.string.red)));
        mColor.add(new Color(getResources().getColor(R.color.colorOrange), getResources().getString(R.string.orange)));
        mColor.add(new Color(getResources().getColor(R.color.colorYellow), getResources().getString(R.string.yellow)));
        mColor.add(new Color(getResources().getColor(R.color.colorLime), getResources().getString(R.string.lime)));
        mColor.add(new Color(getResources().getColor(R.color.colorGreen), getResources().getString(R.string.green)));
        mColor.add(new Color(getResources().getColor(R.color.colorCyan), getResources().getString(R.string.cyan)));
        mColor.add(new Color(getResources().getColor(R.color.colorBlue), getResources().getString(R.string.blue)));
        mColor.add(new Color(getResources().getColor(R.color.colorIndigo), getResources().getString(R.string.indigo)));
        mColor.add(new Color(getResources().getColor(R.color.colorViolet), getResources().getString(R.string.violet)));

        RecyclerView mRecyclerView = view.findViewById(R.id.color_list_view);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        RecyclerView.Adapter mAdapter = new ColorAdapter(mColor, getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);

        return view;
    }

}

Right Fragment

public class RightFragment extends Fragment {

    ColorAdapter     colorAdapter;
    ArrayList<Color> color;

    public RightFragment() {
        // Required empty public constructor
    }

    public static RightFragment newInstance() {
        return new RightFragment();
    }


    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_right, container, false);

        final ConstraintLayout constraintLayout = view.findViewById(R.id.right_color_block);
        final TextView         textView         = view.findViewById(R.id.right_color_text);

        colorAdapter.SetItemClickListener(new ColorAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(int position) {
                constraintLayout.setBackgroundColor(color.get(position).getmColor());
                textView.setText(color.get(position).getmLabel());
            }
        });

        return view;
    }

}

ColorAdaptor (RecyclerView)

public class ColorAdapter extends RecyclerView.Adapter<ColorAdapter.ColorViewHolder> {

    private List<Color>         mColor;
    private Context             mContext;
    private OnItemClickListener mOnItemClickListener;

    public ColorAdapter(List<Color> mColor, Context mContext) {
        this.mColor = mColor;
        this.mContext = mContext;
    }

    public void SetItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @NonNull
    @Override
    public ColorViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.color_list_item, viewGroup, false);
        return new ColorViewHolder(view, mOnItemClickListener);
    }

    @Override
    public void onBindViewHolder(@NonNull final ColorViewHolder colorViewHolder, final int i) {

        final Color color = mColor.get(i);

        colorViewHolder.itemColorText.setText(color.getmLabel());
        colorViewHolder.itemColorText.setBackgroundColor(color.getmColor());

        colorViewHolder.itemColorText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "Item clicked: " + i, Toast.LENGTH_SHORT).show();
//                colorViewHolder.feedbackBlock.setBackgroundColor(color.getmColor());
                colorViewHolder.feedbackText.setText(color.getmLabel());
            }
        });

    }

    @Override
    public int getItemCount() {
        return mColor.size();
    }

    public interface OnItemClickListener {
        void OnItemClick(int position);
    }

    class ColorViewHolder extends RecyclerView.ViewHolder {

        public ConstraintLayout itemColorBlock;
        public TextView         itemColorText;
        public ConstraintLayout feedbackBlock;
        public TextView         feedbackText;

        public ColorViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
            super(itemView);

            itemColorBlock = itemView.findViewById(R.id.color_block);
            itemColorText = itemView.findViewById(R.id.color_text);
            feedbackBlock = itemView.findViewById(R.id.right_color_block);
            feedbackText = itemView.findViewById(R.id.right_color_text);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null) {
                        int position = getAdapterPosition();

                        if (position != RecyclerView.NO_POSITION) {
                            listener.OnItemClick(position);
                        }
                    }
                }
            });

        }
    }

}

Color

public class Color {

    private int mColor;
    private String mLabel;

    public Color(@ColorInt int color, String label) {
        this.mColor = color;
        this.mLabel = label;
    }

    public String getmLabel() {
        return mLabel;
    }

    public void setmLabel(String mLabel) {
        this.mLabel = mLabel;
    }

    public int getmColor() {
        return mColor;
    }

    public void setmColor(@ColorInt int mColor) {
        this.mColor = mColor;
    }

    @NonNull
    @Override
    public String toString() {
        return super.toString();
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {

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

        LeftFragment leftFragment = LeftFragment.newInstance();
        getSupportFragmentManager().beginTransaction().replace(R.id.left_container, leftFragment).commit();

        RightFragment rightFragment = RightFragment.newInstance();
        getSupportFragmentManager().beginTransaction().replace(R.id.right_container, rightFragment).commit();

    }

}
Code Lover
  • 8,099
  • 20
  • 84
  • 154
  • Please look into how to create a [mcve]. The code you've posted doesn't even contain the method call that's throwing the Exception stated in your title. Please also provide the compete stack trace when posting questions like this. – Mike M. Feb 23 '19 at 06:34
  • Sorry but I didn't understand exactly, Which method do you need? I do not have any more Java files. :) – Code Lover Feb 23 '19 at 06:40
  • Please provide crash logs / compete stack trace. – Mayank Sharma Feb 23 '19 at 06:41
  • Your title states that the NPE is coming from a "`SetonItemClickListener`" call. The code you've posted does not contain that method anywhere. – Mike M. Feb 23 '19 at 06:47
  • @MikeM. Oh I see. Let me see. BTW this should I should set into `RightFragment` is that correct? sorry if it is a stupid question but interface is quite confusing to me. – Code Lover Feb 23 '19 at 06:50
  • @MikeM. I have set `SetonItemClickListener` in the `RightFrament` so is there anything else I have to implement? – Code Lover Feb 23 '19 at 06:54
  • "I have set `SetonItemClickListener` in the `RightFrament`..." – No, you haven't. You have a call to `SetItemClickListener()` there, but that's not `SetonItemClickListener`. Notice the `on`. You need to be very specific and exact with your descriptions. – Mike M. Feb 23 '19 at 07:09

1 Answers1

0

Your App Crash on RightFragment.class because you are calling colorAdapter.SetItemClickListener (on line 43) as in **RightFragment colorAdapteris null **, you are setting adapter on the different fragment and implementing a listener on different so that's why this crash happens.

You can apply this listener on LeftFragment (where you originally set adapter) and on click of that(in listener callback) you can open right fragment ( send selected data from LeftFragment to RightFragment), This SO Answer can help you in this , when right fragment open you will get selected color data and here you can use it on RightFragment.

Amir jodat
  • 569
  • 6
  • 13
Mayank Sharma
  • 2,735
  • 21
  • 26