Attempt to invoke virtual method ' 'on a null object reference

前端 未结 5 850
一个人的身影
一个人的身影 2020-12-22 05:37

I am getting the following error

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

5条回答
  •  醉话见心
    2020-12-22 05:58

    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;
    

提交回复
热议问题