0

当有人输入地址以从地址中获取经纬度时,我有一个由编辑文本字段填充的字符串。

    final String specAddressStr = specAddress.getText().toString() + " " + specCity.getText().toString() + "," + " " + specState.getText().toString() + " " + specZip.getText().toString();

我需要在异步任务中使用此字符串,但是当我尝试引用它并将字符串设置为全局变量以在任务中使用它时,它会导致应用程序强制关闭。是否有其他方法可以在我缺少的异步任务中使用动态填充的字符串?这是请求的异步任务代码:

    public class LowSignal extends Activity {

String specAddressStr;

private class processLatandLong extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {


        List<Address> foundGeocode = null;

        // find the addresses by using getFromLocationName() method with the given address

        try {

            foundGeocode = new Geocoder(LowSignal.this).getFromLocationName(specAddressStr, 1);
            foundGeocode.get(0).getLatitude(); // getting latitude
            foundGeocode.get(0).getLongitude();// getting longitude



        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (foundGeocode !=null) {
            returnedLat.setText(String.valueOf(foundGeocode.get(0).getLatitude()));
            returnedLong.setText(String.valueOf(foundGeocode.get(0).getLongitude()));
        } else {
            returnedLat.setText("Unable to find Latitude. Please try again.");
            returnedLong.setText("Unable to find Longitude. Please try again.");
        }


        return null;

    }

这是我调用任务的地方:

    public void getLatandLong(View v) {

    String specAddressStr = specAddress.getText().toString() + " "
                + specCity.getText().toString() + "," + " "
                + specState.getText().toString() + " "
                + specZip.getText().toString();

        new processLatandLong().execute(specAddressStr);

    }
}
4

1 回答 1

1

您可以将 String 传递AsyncTask.execute(Your_String)给以访问 VuedoInBackground为:

    specAddressStr = specAddress.getText().toString() + " " + 
                     specCity.getText().toString() + "," + " " + 
                     specState.getText().toString() + " " + 
                     specZip.getText().toString();
    new LongOperation().execute(specAddressStr);

private class LongOperation extends AsyncTask<String, Void, String> {

      @Override
      protected String doInBackground(String... params) {
               String stredittext=params[0];
            }
// your code....
}
于 2012-11-20T17:12:15.713 回答