2012-11-25 25 views
1

我在這個stackoverflow question的幫助下做了一個簡單的AsyncTask類來在ListView中顯示數據。 但是AsyncTask onPostExecute沒有被調用。使用AsyncTask在ListView中顯示數據,但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); 

     } 
} 
} 

的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 
+0

你在這個地方發現錯誤 - 「lv.setOnItemClickListener(new OnItemClickListener()...」,因爲你之前沒有定義你的「lv」 – nfirex

回答

5

因爲,

ListView lvNULL

你忘了後setContentView(R.layout.test);

你必須做的是這樣來定義ListView lv

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

所以從onPostExecute()

刪除lv = (ListView) findViewById(R.id.ListView);和前行代碼new MyAsyncTask().execute("http://....");把它。

原因:

其實什麼發生的是,

臨睡前的AsyncTask的onPostExecute(),代碼行lv.setOnItemClickListener(new OnItemClickListener() {...}執行和那裏Listview lv對象是NULL

+0

哦,謝謝,現在它工作:) – sumisu

相關問題