我試圖創建一個應用程序,通過GPS獲取我的地理位置並將其發送到數據庫。所有工作正常,直到我第二次嘗試更新我的地理位置。 我正在通過ubuntu終端發送數據到仿真器。 當我第一次發送它時,它將地圖導入該點,並將適當的數據插入到數據庫中,但是當我第二次嘗試發送數據時,它顯示「不幸的是,應用程序已停止」)AsyncTask停止應用程序,當我試圖啓動它第二次
我發送數據的方式:
telnet localhost 5554
geo fix 100 100
代碼
public class LocationActivity extends MapActivity implements LocationListener {
...
InsertToDatabase Insert = new InsertToDatabase();
@Override
public void onLocationChanged(Location location) {
Insert.latitude = location.getLatitude();
Insert.longitude = location.getLongitude();
Insert.execute();
try {
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10);
for (Address address : addresses) {
this.locationText.append("\n" + address.getAddressLine(0));
}
int latitude = (int)(location.getLatitude() * 1000000);
int longitude = (int)(location.getLongitude() * 1000000);
GeoPoint point = new GeoPoint(latitude,longitude);
mapController.animateTo(point);
} catch (IOException e) {
Log.e("LocateMe", "Could not get Geocoder data", e);
}
}
InsertToDatabase類(LocationActivity類的子類)
class InsertToDatabase extends AsyncTask<String, String, String> {
Double latitude;
Double longitude;
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LocationActivity.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("longitude", this.longitude.toString()));
params.add(new BasicNameValuePair("latitude", this.latitude.toString()));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
如果你強制關閉,請張貼logcat的異常 – tolgap
@tolgap我會下一次,對不起,我是新的android – pawel