I’m trying to create an application which gets my geo position via GPS and sends it to database. All works fine until I’m trying to update my geo position for the second time.
I’m sending data into emulator via ubuntu terminal.
When I send it for the first time, it directs map into that point, and insert the proper data into database, but when I try to send the data for the second time it shows “Unfortunately, application has stopped”)
I send data that way:
telnet localhost 5554
geo fix 100 100
Code
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 class (subclass of LocationActivity class)
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();
}
}
You should use
new Insert(..).execute();everytime you want to start an AsyncTask. By definition an AsynTask runs only one time.
Since you create your Insert object just once, your app will crash if you try to run that instance another time.
If you need to pass parameters or data to the Insert, you should write a new constructor, taking these parameters and then call it this way: