I am getting the following error
Attempt to invoke virtual method \'void android.widget.StackView.setAdapter(android.widget.Adapter)\' on a null
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;
R.id.stackView1
is not found when you try to assign stackView
, so it is null
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;
}
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.
change container and false to null
Do this:
View rootView = inflater.inflate(R.layout.events, null);