使用AsyncTask在ListView中显示数据,但不调用onPostExecute

时间:2012-11-25 10:55:42

标签: java android json android-listview android-asynctask

我创建了一个简单的AsyncTask类,在stackoverflow question的帮助下在ListView中显示数据。 但是没有调用AsyncTask onPostExecute。

这是我的代码:

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);

        }
}
}

蚀:

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

1 个答案:

答案 0 :(得分:5)

由于,

您的ListView lv NULL

您忘记在ListView lv

之后定义setContentView(R.layout.test);

你必须做类似的事情,

lv = (ListView) findViewById(R.id.<lisview_id_from_test.xml>);

lv = (ListView) findViewById(R.id.ListView);

中删除onPostExecute()

并将其放在代码行new MyAsyncTask().execute("http://....");之前。

<强>原因:

实际上会发生什么,

在转到AsyncTask的onPostExecute()之前,代码行lv.setOnItemClickListener(new OnItemClickListener() {...}已执行,Listview lv的对象 NULL