-3

コード:

public class OfferDetails extends Activity {

    TextView name_offerdetails;
    TextView start_offerdetails;
    TextView end_offerdetails;
    TextView description_offerdetails;

    String offer_id;

    String message;
    int success;

    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();

    //single product url
    public static final String DOMAIN = "192.168.0.112";
    private static String url_all_offers = "http://" + DOMAIN + "/iChop/get_offer_details.php";

    //JSON Node names
    private static final String TAG_SUCCESS = "success";
    //private static final String TAG_MESSAGE = "message";
    private static final String TAG_OFFER = "offer";
    private static final String TAG_OFFER_ID = "offer_id";
    private static final String TAG_NAME = "m_name";
    private static final String TAG_START = "start_date";
    private static final String TAG_END = "end_date";
    private static final String TAG_DESCRIPTION = "o_description";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_offer_details);

        //Getting offer details from intent
        Intent i = getIntent();

        offer_id = i.getStringExtra(TAG_OFFER_ID);

        new GetOfferDetails().execute();
    }

    class GetOfferDetails extends AsyncTask<String, String, String>{
        @Override
        protected void onPreExecute(){
            super.onPreExecute();
            pDialog = new ProgressDialog(OfferDetails.this);
            pDialog.setMessage("Loading offer details. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        protected String doInBackground(String... params){
            runOnUiThread(new Runnable(){
                public void run(){
                    try{
                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("offer_id", offer_id));

                        JSONObject json = jsonParser.makeHttpRequest(url_all_offers, "GET", params);

                        Log.d("Single Offer details", json.toString());

                        success = json.getInt(TAG_SUCCESS);
                        if(success == 1){
                            JSONArray offerObj = json.getJSONArray(TAG_OFFER);

                            //get first offer objects from JSON array
                            JSONObject offer = offerObj.getJSONObject(0);

                            //offer with this offer_id found
                            name_offerdetails = (TextView) findViewById(R.id.name_offerdetails);
                            start_offerdetails = (TextView) findViewById(R.id.start_offerdetails);
                            end_offerdetails = (TextView) findViewById(R.id.end_offerdetails);
                            description_offerdetails = (TextView) findViewById(R.id.description_offerdetails);

                            //display in text view
                            name_offerdetails.setText(offer.getString(TAG_NAME));
                            start_offerdetails.setText(offer.getString(TAG_START));
                            end_offerdetails.setText(offer.getString(TAG_END));
                            description_offerdetails.setText(offer.getString(TAG_DESCRIPTION));
                        } else {
                            //
                        }
                    } catch (JSONException e){
                        e.printStackTrace();
                    }
                }
            });

            return null;
        }

        protected void onPostExecute(String file_url){
            pDialog.dismiss();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.offer_details, menu);
        return true;
    }

    public void Code (View view) {
        Intent code = new Intent (this, Code.class);
        startActivity(code);
    }

    public void Home (View view) {
        Intent home = new Intent (this, Home.class);
        startActivity(home);
    }

    public void Setting (View view) {
        Intent setting = new Intent (this, Setting.class);
        startActivity(setting);
    }

}

AndroidHive のチュートリアルに従ったところ、このエラーが発生しました。

05-17 18:16:05.905: E/AndroidRuntime(4858): android.os.NetworkOnMainThreadException

私はオンラインでチェックしましたが、runOnUiThread を setText 部分だけに移動するとうまくいくと言う人もいましたが、うまくいきません...どうすればこれを解決できますか?

4

1 に答える 1

0

doInBackground のすべての本体ではなく、runOnUiThread実行する必要があるコードのみの内部に移動する必要があります。UI Threadあなたのコードでのみ

 name_offerdetails = (TextView) findViewById(R.id.name_offerdetails);
 start_offerdetails = (TextView)  findViewById(R.id.start_offerdetails);
 end_offerdetails = (TextView) findViewById(R.id.end_offerdetails);
  description_offerdetails = (TextView) findViewById(R.id.description_offerdetails);

                        //display in text view
  name_offerdetails.setText(offer.getString(TAG_NAME));
  start_offerdetails.setText(offer.getString(TAG_START));
  end_offerdetails.setText(offer.getString(TAG_END));
  description_offerdetails.setText(offer.getString(TAG_DESCRIPTION));

UI スレッドで実行する必要があります。また、doInBackground が JSONObject を返すようにすることもできます。そしてonPostExecute、UI スレッドで実行する必要があるコードを実行します。実際onPostExecuteには常に上で実行されますUI Thread。たとえば、AsyncTask は次のようになります。

 class GetOfferDetails extends AsyncTask<String, String, JSONObject>{
    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        pDialog = new ProgressDialog(OfferDetails.this);
        pDialog.setMessage("Loading offer details. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected JSONObject doInBackground(String... params){

                try{
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("offer_id", offer_id));

                    JSONObject json = jsonParser.makeHttpRequest(url_all_offers, "GET", params);

                    Log.d("Single Offer details", json.toString());

                    success = json.getInt(TAG_SUCCESS);
                    if(success == 1){
                        JSONArray offerObj = json.getJSONArray(TAG_OFFER);

                        //get first offer objects from JSON array
                        JSONObject offer = offerObj.getJSONObject(0);

                        return offer;
                    } else {
                        //
                    }
                } catch (JSONException e){
                    e.printStackTrace();
                }


        return null;
    }

    protected void onPostExecute(JSONObject offer){
        pDialog.dismiss();
        if (offer != null) {
            //offer with this offer_id found
            name_offerdetails = (TextView) findViewById(R.id.name_offerdetails);
            start_offerdetails = (TextView) findViewById(R.id.start_offerdetails);
            end_offerdetails = (TextView) findViewById(R.id.end_offerdetails);
            description_offerdetails = (TextView) findViewById(R.id.description_offerdetails);

                        //display in text view
             name_offerdetails.setText(offer.getString(TAG_NAME));
             start_offerdetails.setText(offer.getString(TAG_START));
             end_offerdetails.setText(offer.getString(TAG_END));
             description_offerdetails.setText(offer.getString(TAG_DESCRIPTION));
     }
    }
}
于 2013-05-17T10:36:38.707 に答える