James James - 5 months ago 9
Java Question

Unknown fatal exception at Runtime when using Fragments and lists

Afternoon all,

I'm currently learning all about fragments and their use in android apps. I'm trying to populate a listView with an array of Strings. This sort of idea worked when I was populating the list without using fragments but at the moment it's throwing a runtime exception "Unable to start activity". A simple point at where to look would be much appreciated as I can't figure out the logcat reading.

The fragment is thus:

public class setupFragment extends Fragment {

public void onAttach(Context context) {

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

View view = inflater.inflate(R.layout.fragment_setup, container, false);

ListView listView = (ListView)getView().findViewById(R.id.listView); //the program seems to at this point

String[] setups = getResources().getStringArray(R.array.setups);

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, setups);


return view;

It's my first time posting java fragments, so any posting errors I apologize for. Cursory breakpoint debugging indicates that the program is breaking on the ListView declaration.

I can post some of the logcat on request, I'm not posting it at the moment due it being long and convoluted.


Replace: ListView listView = (ListView)getView().findViewById(R.id.listView);

with: ListView listView = (ListView) view.findViewById(R.id.listView);

You can't call getView() before returning one from onCreateView().

getView() returns null.