You should inflate the fragment's view in the fragment code only, not in the activity code. You use onCreateView
method to do this.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, container, false);
ListView myListView= (ListView) v.findViewById(R.id.myListView);
return v;
}
If you want to find your ListView
after onCreateView
has already been called, you can use getView()
method:
ListView myListView= (ListView) getView().findViewById(R.id.myListView);