0

需要你的帮助!!!我有活动,我在其中获取一些 JSON 格式的数据并将其放入 ListView。一切正常。但我想用 Setonitemclicklistener 定义在列表中单击了哪些项目。我试图在我的内部 Connection 类的 onPostExecuted() 方法中使用它,它扩展了 AsyncTask,但我有 NullPoinerException

public class ConnectionActivity extends ListActivity{
JsonParser jsonParser =  new JsonParser(); 
private ListView listView;
JSONArray inbox = null;
  private ProgressDialog pDialog;
private static final String TAG_ID = "ID";
private static final String TAG_NAME = "Date";
private static final String TAG_DATE = "Name";
private static final String TAG_PRICE = "Price";
ArrayList<HashMap<String,String>> resultList;

public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 resultList = new ArrayList<HashMap<String,String>> ();
 new Connection().execute();
 new ListHandler().execute();

}
class Connection extends AsyncTask<String,String,String>
{

    @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ConnectionActivity.this);
            pDialog.setMessage("Не базарь и жди пока загрузиться!!!!");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

    @Override
    protected String doInBackground(String... args) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        // getting JSON string from URL
        String json = jsonParser.makeHttpRequest();
        Log.d("Outbox JSON",json.toString());

        try{

            JSONArray jArray = new JSONArray(json);
            for (int i=0;i<jArray.length();i++) {
                    JSONObject c = jArray.getJSONObject(i);


                 // Storing each json item in variable
                    String id = c.getString(TAG_ID);
                    String name= c.getString(TAG_NAME);
                    String date = c.getString(TAG_DATE);
                    String price = c.getString(TAG_PRICE);

                    // creating new HashMap
                    HashMap<String, String> hashmap = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    hashmap.put(TAG_ID, id);
                    hashmap.put(TAG_NAME, name);
                    hashmap.put(TAG_DATE, date);
                    hashmap.put(TAG_PRICE, price);

                    // adding HashList to ArrayList
                    resultList.add(hashmap);

            }

        }
        catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
        }
        return null;
    }
     protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            ConnectionActivity.this, resultList,
                            R.layout.connect_item, new String[] { TAG_ID, TAG_NAME, TAG_DATE, TAG_PRICE },
                            new int[] { R.id.order_id, R.id.order_date, R.id.order_name, R.id.order_price });
                    // updating listview
                    setListAdapter(adapter);
                    listView =  (ListView) findViewById(R.id.list);
                    listView.setOnItemClickListener(new ListClickListener());
                }
            });

        }
}

我认为问题在于使用一个线程,所以我在我的 ConnectionActivity 中创建了一个内部类,如下所示^

 class ListHandler extends AsyncTask<Void,Void,Void>
     {
        protected void onPreExecute ()
         {


         }
        @Override
        protected Void  doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        protected void onPostExecute()
        {
            listView =  (ListView) findViewById(R.id.list);

            listView.setOnItemClickListener(new OnItemClickListener() {

           public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
               Log.d("Click on the item","!!!!!!!!!!!!!!!!!!!!!");
               Toast toast = Toast.makeText(getApplicationContext(), 
                       "Пора покормить кота!", Toast.LENGTH_SHORT); 
                    toast.show(); 
           } 
        });


        }


     }

但它不起作用。我的意思是当我点击 item 时我无法得到 Toast,但也不例外。我试图在 Activity 的 void onResume() 方法中处理 setonitemclicklistener,但我得到了 NullPointerException。我也在 ListHandler 类的 OnPreExecute() 方法中处理了这个 - 结果相同......请帮忙......

4

2 回答 2

0

在哪里setContentView(R.layout.yourxml)?我认为您在 oncreate 方法中忘记了它。

所以你的listView is null and gives NPE.

onPostExecute method have runonUiThread,Remove it no need it.它已经在 UI 线程中运行。

于 2012-07-19T10:31:44.907 回答
0

通过从 Activity 扩展而不是 ListActivity 并使用 setContentView() 解决了问题。

于 2012-08-14T08:07:10.693 回答