【发布时间】:2011-12-15 18:34:52
【问题描述】:
我有我的应用程序来查找位置工作,但我更改了一些代码,现在位置总是返回 null。很惭愧,我不确定我更改了哪些代码,但我似乎无法让它工作。
任何人都可以看到为什么返回 null 吗?我整天都在为此工作,没有任何乐趣。 代码如下:
....imports....
public class Clue extends Activity {
public static double latitude;
public static double longitude;
Criteria criteria;
LocationManager lm;
LocationListener ll;
Location location;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questions);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
ll = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
areWeThereYet();
}
private void areWeThereYet()
{
if(weAreThere())
{
showMsgBox("There");
}
else if(location!=null)
toastMsg("not there");
}
private boolean weAreThere() {
location = getLocation();
if (location!=null)
{
longitude = location.getLongitude();
latitude = location.getLatitude();
return inCorrectPlace(question);
}
else
{
toastMsg("Location not ready yet");
return false;
}
}
private Location getLocation() {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
return lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc)
{
Clue.longitude = loc.getLongitude();
Clue.latitude = loc.getLatitude();
}
}
我已经添加了所有权限并简化了我的代码,所以你们都可以看到发生了什么。
谢谢,
本
【问题讨论】:
-
只是对未来的建议:将您的代码置于版本控制中!
-
我通常这样做,这是一个小项目,所以我没有打扰:S 希望我现在这样做!
-
您是在设备还是模拟器上执行此操作?如果是设备,请检查以确保 GPS 未关闭。如果是模拟器,我认为您需要使用 DDMS 至少发送一次位置,以便 locationManager 有位置返回?
标签: java android location locationmanager