Skip to content Skip to sidebar Skip to footer

NullPointerException On OnCreateView When Using Fragment And Listview

I have converted Many Activities to Fragment, all are working fine except one. It gives Null Pointer Exception on setOnItemClickListener. I have checked many similar questions, but

Solution 1:

@android:id/list uses the ID "list" from the Android package, so you need to reference it as findViewById(android.R.id.list) in your Java code.


Solution 2:

Try this:

View fragment = inflater.inflate(R.layout.your_fragment_layout, container, false);
lv = (ListView) fragment.findViewById(android.R.id.list);

Solution 3:

Your ArrayList needs to be like this:

ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>

Instead of :

llLayout = (RelativeLayout) inflater.inflate(R.layout.listnewsjson, container, false);

insert this:

View v = inflater.inflate(R.layout.listnewsjson, container, false);
lv = (ListView) v.findViewById(android.R.id.list);


...
new GetContacts().execute();
return v;

Your AsyncTask needs to be like this:

AsyncTask<Void, Void, ArrayList<HashMap<String, String>>

Your doInBackground needs to be like this:

ArrayList<HashMap<String, String>> doInBackground(Void... params)

Your onPostExecute needs to be like this:

onPostExecute(ArrayList<HashMap<String, String>> result)

set adapter:

lv.setAdapter(new SomeAdapter(getActivity().getApplicationContext(), result));
...

Post a Comment for "NullPointerException On OnCreateView When Using Fragment And Listview"