Android 如何在方法doInBackground中传递参数纬度和经度?

Android 如何在方法doInBackground中传递参数纬度和经度?,android,android-asynctask,reverse-geocoding,Android,Android Asynctask,Reverse Geocoding,我尝试对AsyncTask使用反向地理编码,但是通过doInBackground()方法传递的纬度和经度参数没有正确发生。有什么想法吗 public class SitesAdapter extends ArrayAdapter<StackSite> { public static Double lat; public static Double lng; @Override public View getView(int pos, View co

我尝试对AsyncTask使用反向地理编码,但是通过doInBackground()方法传递的纬度和经度参数没有正确发生。有什么想法吗

public class SitesAdapter extends ArrayAdapter<StackSite> { 
    public static Double lat;
    public static Double lng;

    @Override
    public View getView(int pos, View convertView, ViewGroup parent){
    ...
        lat = -1.80; 
        lng = -80.20;
    ...
    }
    public void start(){
        new GetAddressTask(mContext).execute(lat, lng);  
    }

    public static class GetAddressTask extends AsyncTask<Double, Void, String> {
        //mContext            

        @Override
        protected String doInBackground(Double... params) {
            Geocoder gc = new Geocoder(mContext, Locale.getDefault());          
            List<Address> list = null;
            String city = "";
            double latitude = params[0];
            double longitude = params[1];           
            try {
                list = gc.getFromLocation(lat, lng, 1);             
            } catch (IOException e) {               
                e.printStackTrace();                
            }               
            if (list != null && list.size() > 0) {
                Address address = list.get(0);
                city = String.format("%s, %s", address.getAdminArea(), address.getCountryName());                             
            }
            return city;            
        }

        @Override
        protected void onPostExecute(String city) {         
            tituloTxt.setText(city);
        }
    }
}

在类中添加两个变量,并在创建异步任务时设置它们,然后在方法中使用它们。简单

public class GetAddressTask extends AsyncTask<String, Void, String> {
        Context mContext;
        float lat,lin;

        public void setLat(int lat){...}

       //rest of class
公共类GetAddressTask扩展了AsyncTask{
语境;
浮动拉特,林;
公共void setLat(int lat){…}
//其他同学
当然,您可以将所有内容都设置为静态(字段和设置器)

编辑


如果使用某些参数调用execute,请记住,在调用execute之前必须设置值。

在调用execute方法之前,您可以创建一个构造函数,在该构造函数中可以初始化类数据成员,这些成员可以在doInBackground(..)中进一步使用.

之后,只需执行此操作即可传递坐标。首先将坐标添加到构造函数LatLng(双纬度,双经度)并传递参数

    lat = -1.80; 
    lng = -80.20;
    LatLng latlng = new LatLng(lat, lng);
    new GetAddressTask(mContext).execute(lat, lng);
然后在doInbackground方法内部获取参数

public static class GetAddressTask extends AsyncTask<LatLng, Void, String> {
    //mContext            

    @Override
    protected String doInBackground(LatLng... params) {
        Geocoder gc = new Geocoder(mContext, Locale.getDefault());          
        List<Address> list = null;
        String city = "";
        LatLng loc = params[0]; //Get all parameters: latitude and longitude         
        try {
          list = gc.getFromLocation(loc.latitude, loc.longitude, 1); //get specific parameters                
        } catch (IOException e) {           
           e.printStackTrace();              
        }
        if (list != null && list.size() > 0) {
          Address address = list.get(0);
          city = String.format("%s, %s", address.getAdminArea(), address.getCountryName());
          return city;
       }else{
          return "City no found";
       }                
    }

    @Override
    protected void onPostExecute(String city) {         
        tituloTxt.setText(city);
    }
}
公共静态类GetAddressTask扩展了AsyncTask{
//McContext
@凌驾
受保护管柱后台(LatLng…参数){
Geocoder gc=新的地理编码器(mContext,Locale.getDefault());
List=null;
字符串城市=”;
LatLng loc=params[0];//获取所有参数:纬度和经度
试一试{
list=gc.getFromLocation(loc.latitude,loc.longitude,1);//获取特定参数
}捕获(IOE){
e、 printStackTrace();
}
if(list!=null&&list.size()>0){
地址=list.get(0);
city=String.format(“%s,%s”,address.getAdminArea(),address.getCountryName());
回归城市;
}否则{
返回“城市未找到”;
}                
}
@凌驾
受保护的void onPostExecute(字符串城市){
tituloTxt.setText(城市);
}
}

Basic Java。你熟悉字段、setter和getter吗?RTFM。就在这里:你提供的错误是无用的。堆栈跟踪在哪里?一些有用的信息?你不知道你在做什么。或者task.execute(lat,lng);我试图传递坐标并执行异步任务,但我有一个错误。我在上面更新了。
public static class GetAddressTask extends AsyncTask<LatLng, Void, String> {
    //mContext            

    @Override
    protected String doInBackground(LatLng... params) {
        Geocoder gc = new Geocoder(mContext, Locale.getDefault());          
        List<Address> list = null;
        String city = "";
        LatLng loc = params[0]; //Get all parameters: latitude and longitude         
        try {
          list = gc.getFromLocation(loc.latitude, loc.longitude, 1); //get specific parameters                
        } catch (IOException e) {           
           e.printStackTrace();              
        }
        if (list != null && list.size() > 0) {
          Address address = list.get(0);
          city = String.format("%s, %s", address.getAdminArea(), address.getCountryName());
          return city;
       }else{
          return "City no found";
       }                
    }

    @Override
    protected void onPostExecute(String city) {         
        tituloTxt.setText(city);
    }
}