2012-09-07 29 views
0

我试图创建一个应用程序,通过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(); 
     } 

    } 
+2

如果你强制关闭,请张贴logcat的异常 – tolgap

+0

@tolgap我会下一次,对不起,我是新的android – pawel

回答

1

您应该使用

new Insert(..).execute(); 每次要启动的AsyncTask。根据定义,AsynTask只运行一次。

由于您只需创建一次Insert对象,如果您尝试再次运行该实例,则您的应用程序将崩溃。

如果您需要的参数或数据传递给插入,你应该写一个新的构造,服用这些参数,然后调用这样说:

new Insert(longitude,latitude).execute(); 
+0

谢谢:)现在很清楚。 – pawel

相关问题