0

I have implemented a RecyclerView with a custom adapter that adds a my own custom objects made from the my class Item. Now I am trying to add a Spinner that will have a list of Strings where when you click on an object in the Spinner drop down menu, it will change the content of the RecyclerView. Basically where each choice in the Spinner will have its own list for the RecyclerView. But when I try to add content to the Spinner, the app crashes when I try to start it. This is the code I added to onCreate(Bundle savedInstanceState) that made the app crashed:

  ArrayList<String> lists = new ArrayList<>();
  lists.add("firstList");
  lists.add("secondList");
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, lists);
  dropdown.setAdapter(adapter);

I thought it might be crashing because the ArrayAdpater somehow conflicts with my custom ItemAdapter, but I am not sure because I am new to using adapters. Does anyone know how I can resolve this?

If it will help I have my custom adapter here:

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ExampleViewHolder> {
    private ArrayList<Item> mItemList;
    private OnItemClickListener mListener;
    private int selectedPosition=-1;

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

    public void setOnItemClickListener(OnItemClickListener listener){
        mListener = listener;
    }

    public static class ExampleViewHolder extends RecyclerView.ViewHolder{

        public TextView mTextView1;
        public TextView mTextView2;

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

            mTextView1 = itemView.findViewById(R.id.text1);
            mTextView2 = itemView.findViewById(R.id.text2);

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

                        }
                    }
                }
            });
        }
    }

    public ItemAdapter(ArrayList<Item> itemList) {
        mItemList = itemList;
    }


    @NonNull
    @Override
    public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.example_item, viewGroup, false);
        ExampleViewHolder evh = new ExampleViewHolder(v, mListener);
        return evh;
    }

    @Override
    public void onBindViewHolder(@NonNull ExampleViewHolder exampleViewHolder, final int position) {
        Item currentItem = mItemList.get(position);

        exampleViewHolder.mTextView1.setText(currentItem.getText1());
        exampleViewHolder.mTextView2.setText(currentItem.getText2());

        if(selectedPosition==position)
            exampleViewHolder.itemView.setBackgroundColor(Color.GREEN);
        else
            exampleViewHolder.itemView.setBackgroundColor(Color.parseColor("#ffffff"));
        exampleViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.positionOfChosenItem = position;
                selectedPosition = position;
                MainActivity.btnChange.setVisibility(View.VISIBLE);
                MainActivity.btnDelete.setVisibility(View.VISIBLE);
                MainActivity.changeAmountField.setVisibility(View.VISIBLE);

                notifyDataSetChanged();
            }
        });
    }

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

EDIT: I have made sure that the variables are set to the right value like setting dropdown to the Spinner in the layout. dropdown = findViewById(R.id.spinner); And have set contentView to the layout, that the Spinner lies in. setContentView(R.layout.activity_main);

EDIT: Here is the error as well:

    E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.barxqrscanner2, PID: 20357
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.barxqrscanner2/com.example.barxqrscanner2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
            at android.app.ActivityThread.-wrap11(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
            at android.os.Handler.dispatchMessage(Handler.java:105)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6938)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
            at com.example.barxqrscanner2.MainActivity.buildDropDown(MainActivity.java:123)
            at com.example.barxqrscanner2.MainActivity.onCreate(MainActivity.java:61)
            at android.app.Activity.performCreate(Activity.java:7174)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030) 
            at android.app.ActivityThread.-wrap11(Unknown Source:0) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
            at android.os.Handler.dispatchMessage(Handler.java:105) 
            at android.os.Looper.loop(Looper.java:164) 
            at android.app.ActivityThread.main(ActivityThread.java:6938) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 
Application terminated.

1 Answers1

0

Your error (NullPointerException) is due to this line:

dropdown.setAdapter(adapter);

Your object dropdown seems to be null.

I think you forgot to instantiate this object, or you are calling setAdapter() before instantiating dropdown. But without the rest of the code, it is difficult to tell more precisely what's the real problem.

Best

Maxouille
  • 2,729
  • 2
  • 19
  • 42