我有一个从AsyncTask
扩展的类,并从Web服务获取JSON格式的数据。每当位置发生变化时,我的Android应用程序都必须异步检索数据。我的android活动implements LocationListener
,我在onLocationChanged()
方法中调用异步执行。我尝试为requestLocationUpdates()
方法的minTime和minDistance参数设置为零,但是永远不会检索要异步检索的数据。
我的onCreate()
和onLocationChanged()
方法的代码如下所示
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nearby);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
// Hashmap for ListView
albumsList = new ArrayList<HashMap<String, String>>();
}
@Override
public void onLocationChanged(Location location) {
latitude = String.valueOf(location.getLatitude());
longitude = String.valueOf(location.getLatitude());
new MyAsyncTask().execute();
}
纬度和经度变量在MyAsyncTask类中用于获取与用户当前位置有关的数据。
请告诉我我的代码有什么问题。自上周以来我一直在寻找解决方案,但没有找到任何解决方案。
答案 0 :(得分:0)
确实试过调试? 要么 登录? 你收到任何错误???
有时location.getLatitute()
和location.getLatitute()
会返回null
,这可能会导致空指针异常 ..
可能停止到达MyAsyncTask()
检查这可能对你有帮助..
@Override
public void onLocationChanged(Location location) {
double latit =null;
double longi =null;
do
{
latit=location.getlatitude();
longi=location.getlongitude();
}while(latit!=null && longi!=null);
latitude = String.valueOf(location.getLatitude());
longitude = String.valueOf(location.getLatitude());
new MyAsyncTask().execute();
}
getlatitude()和getlongitude()函数将返回双值检查此文档
http://developer.android.com/reference/android/location/Location.html#getLatitude()
答案 1 :(得分:0)
答案 2 :(得分:0)
哎呀我的坏!
正确的代码是
@Override
public void onLocationChanged(Location location) {
latitude = String.valueOf(location.getLatitude());
longitude = String.valueOf(location.getLongitude());
new MyAsyncTask().execute();
}