コード:
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 部分だけに移動するとうまくいくと言う人もいましたが、うまくいきません...どうすればこれを解決できますか?