Using AsyncTask to display data in ListView, but onPostExecute not being called
Posted
by
sumisu
on Stack Overflow
See other posts from Stack Overflow
or by sumisu
Published on 2012-11-25T10:55:41Z
Indexed on
2012/11/25
11:03 UTC
Read the original article
Hit count: 377
I made a simple AsyncTask class to display data in ListView with the help of this stackoverflow question. But the AsyncTask onPostExecute is not being called.
This is my code:
public class Start extends SherlockActivity {
// JSON Node names
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
// category JSONArray
JSONArray category = null;
private ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
new MyAsyncTask().execute("http://....");
// Launching new screen on Selecting Single ListItem
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.mail)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra("categoryname", name);
System.out.println(cost);
in.putExtra("categoryid", cost);
startActivity(in);
}
});
}
public class MyAsyncTask extends AsyncTask<String, Void, ArrayList<HashMap<String, String>> > {
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
@Override
protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
category = jParser.getJSONArrayFromUrl(params[0]);
try {
// looping through All Contacts
for(int i = 0; i < category.length(); i++){
JSONObject c = category.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
return contactList;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
ListAdapter adapter = new SimpleAdapter(Start.this, result , R.layout.list_item,
new String[] { TAG_NAME, TAG_ID },
new int[] { R.id.name, R.id.mail });
// selecting single ListView item
lv = (ListView) findViewById(R.id.ListView);
lv.setAdapter(adapter);
}
}
}
Eclipse: 11-25 11:40:31.896: E/AndroidRuntime(917): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.essentials/de.main.Start}: java.lang.NullPointerException
© Stack Overflow or respective owner