我有我的应用程序查找位置的工作,但我改变了一些代码,现在所在的位置总是返回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();
}
}
我添加了所有的权限和简化我的代码,所以你都可以看到发生了什么事情。
谢谢,
Ben