1

在这个stackoverflow question的帮助下,我制作了一个简单的 AsyncTask 类来在 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
4

1 回答 1

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://....");

原因:

实际上发生的事情是,

在去onPostExecute()AsyncTask 之前,代码行 lv.setOnItemClickListener(new OnItemClickListener() {...}被执行并且那里的 ObjectListview lvNULL

于 2012-11-25T10:56:53.130 回答