Reputation: 763
I am trying to define a listview with a corresponding Adapter to add string to the listview. Howevewr, my listview comes as being null. The code is as below:-
public class CategoryFragment extends Fragment {
....
....
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ListView listView = (ListView) getActivity().findViewById(R.id.cat_vacancies);
listAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_2);
listView.setAdapter(listAdapter);
....
}
}
What have I done wrong at this point?
my fragment xml is as follows( I have only added the relevant portions which I think may help in diagnosing the problem)
<RelativeLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cat_vacancies">
</ListView>
</RelativeLayout>
P.S. This is, as far as I know, a silly mistake, but I have not yet completely grasped android completely, so excuse my problems.
Upvotes: 0
Views: 232
Reputation: 391
Try whit that way
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.yourFragmentXml, container, false);
ListView listView = (ListView) view.findViewById(R.id.cat_vacancies);
listAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_2);
listView.setAdapter(listAdapter);
....
}
Upvotes: 1
Reputation: 3201
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.[layout id], container, false);
ListView listView = (ListView) view.findViewById(R.id.cat_vacancies);
retrun view;
}
Upvotes: 1