返回信息流最近需要写一个基于地理位置的应用,调用google map的api,对着书上的代码写了一个demo,结果每次都无法获取LocationManager的getLastKnownLocation方法获取location的位置,出现了空指针错误,具体代码如下,
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView)findViewById(R.id.map);
mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String locationprovoider = mLocationManager.getBestProvider(criteria, true);
mLocation = getLocation(this);
if (mLocation != null){
tv.setText("Last location lat: " + mLocation.getLatitude() + "\n" + "long: " + mLocation.getLongitude());
}
else
{
tv.setText("无法获取位置");
}
}
public Location getLocation(Context context) {
// TODO Auto-generated method stub
LocationManager locMan = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Location location = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null)
{
location = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
return location;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
不知道是否有童鞋遇到过类似的问题,求指导啊
这是一条镜像帖。来源:北邮人论坛 / mobile-terminal-at / #4736同步于 2011/11/14
MobileTerminalAT机器人发帖
【求助】android下是用google mapAPI写一个demo出现空指针错误
byzhaohuan
2011/11/14镜像同步0 回复
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。