0

我遇到了这个错误只有创建视图层次结构的原始线程才能触及它的视图。但我不知道如何在多线程中处理 ui,比如列表视图,我不知道把它放在哪里。

 // Download JSON in Background
        public class DownloadJSONFileAsync extends AsyncTask<String, Void, Void> {

            protected void onPreExecute() {
                super.onPreExecute();
                showDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
            }

            @Override
            protected Void doInBackground(String... params) {


                deviceId = generateDeviceId();

                elementsList.add(new BasicNameValuePair("DeviceID", deviceId));

                JSONObject json = jsonParser.makeHttpRequest(
                        url_get_reports, "POST", elementsList);


                Log.d("Create Response", json.toString());

                // check for success tag
                try {
                    int success = json.getInt(TAG_SUCCESS_REPORT);

                        // successfully created
                        // getting JSON string from URL

                        try {

                            if (success == 1) {

                                reports = json.getJSONArray(TAG_Report);



                                report_data = new Report[reports.length()];





                                // looping through All Products
                                for (int i = reports.length()-1; i >= 0; i--) {
                                    JSONObject c = reports.getJSONObject(i);
                                    reportID = c.getString("reportID");

                                     Drawable drawable = LoadImageFromWeb(url+c.getString(TAG_IMAGE));

                                String state = "";

                                if(c.getString("state").equals("1")){
                                    state = "Pinding";
                                }else if(c.getString("state").equals("2")){
                                    state = "Inprogress";
                                }else{
                                    state = "Completed";
                                }


                                    if(c.getString(TAG_TITLE).equals("")){
                                        report_data[i] = new Report(reportID,"Report "+(i+1),c.getString(TAG_TIME),drawable,url+c.getString(TAG_IMAGE),state,c.getString("coordination")) ;

                                    }else{
                                        report_data[i] = new Report(reportID,c.getString(TAG_TITLE),c.getString(TAG_TIME),drawable,url+c.getString(TAG_IMAGE),state,c.getString("coordination"));

                                    }




                                   if(json.getInt("successReplies")==1){


                                        replies = json.getJSONArray(TAG_REPLIES);



                                        ArrayList<String> arr = new ArrayList<String>();


                                        for(int j=0;j<replies.length();j++){
                                        JSONObject c2 = replies.getJSONObject(j);

                                        if(reportID.equals(c2.getString("ReportID")))
                                        {

                                            arr.add(c2.getString("RepliesContent")+"\n"+c2.getString("RepliesTime"));

                                        }

                                        }


                                   report_data[i].setRepliesArray(arr);


                                   }    


                                }



                                list = (ListView)findViewById(R.id.list);
                                ReportAdapter adapter = new ReportAdapter(Display_Reports.this,report_data);

                                list.setAdapter(adapter);


                            // Click event for single list row
                                list.setOnItemClickListener(new OnItemClickListener() {
                                    @Override
                                    public void onItemClick(
                                            AdapterView<?> arg0, View arg1,
                                            int position, long id) {
                                        Intent intent = new Intent(Display_Reports.this, Display_Report_Details.class);
                                        intent.putExtra("ID", report_data[position].ID);
                                        intent.putExtra("Name", report_data[position].Name);
                                        intent.putExtra("State", report_data[position].State);
                                        intent.putExtra("Time", report_data[position].Time);
                                        intent.putExtra("ImageUrl", report_data[position].ImageUrl);
                                        intent.putExtra("Coordination", report_data[position].Coordination);
                                        intent.putStringArrayListExtra("RepliesContent", report_data[position].getRepliesArray());
                                        startActivity(intent);

                                    }
                                }); 

                            } else {
                                // no Entities found
                                Toast.makeText(Display_Reports.this, "No reports found",
                                        Toast.LENGTH_LONG).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }





                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return null;

            }

            protected void onPostExecute(Void unused) {

                dismissDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
                removeDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);

            }


        }
4

1 回答 1

1

您不能从后台线程修改 UI。是否使用AsyncTask, 或 a Handler, or runOnUiThread(), or post(), 在主应用程序线程上完成工作取决于您。

您可能希望查看有关线程和其中一些技术的文档

于 2013-04-27T15:06:10.720 回答