0

I am getting the following error

Attempt to invoke virtual method 'void android.widget.StackView.setAdapter(android.widget.Adapter)' on a null object reference

on this line

stackView.setAdapter(adapter);

The complete fragment EventsFragment.java is

public class EventsFragment extends android.support.v4.app.Fragment {
    private StackView stackView;
    private  ArrayList<Stack_Items> list;
    TypedArray eventLogo ;
    String eventName[];

    @Nullable
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        eventLogo= getResources().obtainTypedArray(R.array.event_stack_icon);
        eventName = getResources().getStringArray(R.array.event_stack);
        stackView = (StackView) getActivity().findViewById(R.id.stackView1);
        list = new ArrayList<Stack_Items>();

        //Adding items to the list
        for (int i = 0; i < eventLogo.length(); i++) {
            list.add(new Stack_Items(eventName[i], eventLogo.getResourceId(i,-1)));
        }
        //Calling adapter and setting it over stackView
        Stack_Adapter adapter = new Stack_Adapter(getActivity().getApplicationContext(), list );
        stackView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        return inflater.inflate(R.layout.events_layout, null);
    }
}

Stack_Adapter.java

public class Stack_Adapter extends BaseAdapter {

    ArrayList<Stack_Items> arrayList;
    LayoutInflater inflater;
    ViewHolder holder = null;

    public Stack_Adapter(Context context, ArrayList<Stack_Items> arrayList) {
        this.arrayList = arrayList;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Stack_Items getItem(int pos) {
        return arrayList.get(pos);
    }

    @Override
    public long getItemId(int pos) {
        return pos;
    }

    @Override
    public View getView(int pos, View view, ViewGroup parent) {
        if (view == null) {
            view = inflater.inflate(R.layout.stack_layout, parent, false);
            holder = new ViewHolder();

            holder.text = (TextView) view.findViewById(R.id.textView1);
            holder.image = (ImageView) view.findViewById(R.id.imageView1);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.text.setText(arrayList.get(pos).getName());
        holder.image.setBackgroundResource(arrayList.get(pos).getImage());

        return view;
    }

    public class ViewHolder {
        TextView text;
        ImageView image;
    }

}

Stack_Items

public class Stack_Items {
    String name;
    Integer image;

    public Stack_Items(String name, Integer image) {
        this.name = name;
        this.image = image;
    }

    public String getName() {
        return name;

    }

    public int getImage() {

        return image;
    }

}
mergenchik
  • 1,129
  • 16
  • 27
Aman Arora
  • 109
  • 1
  • 11

5 Answers5

3

You are doing:

stackView = (StackView) getActivity().findViewById(R.id.stackView1);

Your stackView is null. getActivity().findViewById returns null.

Why are you using getActivity()?

Where is the stackView? You should load it from the right xml.

as @Tauqir mentioned, you need to inflate the right xml like this:

View view = inflater.inflate(R.layout.events_layout, null);
stackView = (StackView) view.findViewById(R.id.stackView1);
return view;
Thorn
  • 4,015
  • 4
  • 23
  • 42
Shahar
  • 3,692
  • 4
  • 25
  • 47
1

Try this inside onCreateView

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.events_layout, null);

        -------------some codes ------------

        stackView = (StackView) view.findViewById(R.id.stackView1);

        -------------some codes ------------
        return view;

    }
Mohammad Tauqir
  • 1,817
  • 1
  • 18
  • 53
0

R.id.stackView1 is not found when you try to assign stackView, so it is null

mergenchik
  • 1,129
  • 16
  • 27
Jfreu
  • 511
  • 5
  • 10
0

Do this

View rootView = inflater.inflate(R.layout.events, container, false);
stackView = (StackView) rootview.findViewById(R.id.stackView1);
.
.
.
return rootview

You first need to inflate the layout and call findViewById() on the View it returns, thus getActivity().findViewById should be rootView.findViewByID.

There are some other issues with your code as well. Like getctivity().getApplicationContext, just getActivity() is fine.

varunkr
  • 5,364
  • 11
  • 50
  • 99
0

change container and false to null

Do this:

View rootView = inflater.inflate(R.layout.events, null);
aminography
  • 21,986
  • 13
  • 70
  • 74
Wisam Atil
  • 91
  • 1
  • 9