Android - getListView() error in ListFragment

Cam Connor picture Cam Connor · Aug 2, 2013 · Viewed 7.9k times · Source

I am using a ListFragment in my app and I want to make it so that there is a OnItemClick Listener for each item in the list but when I add ListView lv = getListView(); I get an error. If I remove that the listview works But then there will be no onclick listener which I need for my App.

Below is my code

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

//...       

    ListAdapter adapter = new SimpleAdapter(getActivity(), menuItems,
            R.layout.list_item,
            new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
                    R.id.name, R.id.desciption, R.id.cost });

    setListAdapter(adapter);

    ListView lv = getListView();
//HERE IS WHERE I AM GETTING THE ERROR

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
            String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
            String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();

            // Starting new intent
            Intent in = new Intent(getActivity().getApplicationContext(), SingleMenuItemActivity.class);
            in.putExtra(KEY_NAME, name);
            in.putExtra(KEY_COST, cost);
            in.putExtra(KEY_DESC, description);
            startActivity(in);

        }
    });

    return super.onCreateView(inflater, container, savedInstanceState);
}

Logcat is below

08-01 23:24:47.152: E/AndroidRuntime(18883): FATAL EXCEPTION: main
08-01 23:24:47.152: E/AndroidRuntime(18883): java.lang.IllegalStateException: Content view not yet created
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at com.OptimusApps.stayhealthy.AndroidXMLParsingActivity.onCreateView(AndroidXMLParsingActivity.java:74)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1470)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:925)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1102)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1458)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:438)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.os.Handler.handleCallback(Handler.java:605)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.os.Looper.loop(Looper.java:137)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.app.ActivityThread.main(ActivityThread.java:4575)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at java.lang.reflect.Method.invoke(Method.java:511)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at dalvik.system.NativeStart.main(Native Method)

Thanks

Answer

Tizianoreica picture Tizianoreica · Aug 2, 2013

Just put your code in onActivityCreated() and manage your listview from there with simple getListView() method..