【发布时间】:2012-12-17 15:26:01
【问题描述】:
我的问题是我无法使用 LocationManager 类和 LocationListener 获取 GPS 坐标。这是我在 OnCreate() 中调用的代码:
locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
currentLocListener = new MyLocationListener();
locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, currentLocListener);
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, currentLocListener);
我有一个类 MyLocationListener 实现类 LocationListener
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
String coordinates[] = {""+location.getLatitude(),""+location.getLongitude()};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
GeoPoint p = new GeoPoint((int)(lat*1E6), (int)(lng*1E6));
mc.animateTo(p);
mc.setZoom(19);
MyMapOverlays marker = new MyMapOverlays(p);
List<Overlay> listofOverLays = mapview.getOverlays();
listofOverLays.clear();
listofOverLays.add(marker);
mapview.invalidate();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "GPS is disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "GPS is Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}}
我可以通过在 DDMS 的 Emulator Control 中模拟位置来获取位置。所以我尝试在我的真实手机上进行测试。当我安装在手机上时,虽然我打开了 GPS 和 wifi,但它无法自动获取当前位置。
我尝试在我的真实手机上运行另一个谷歌地图应用程序,它运行良好。我不明白代码有什么问题...谁能帮助我?
LOGCAT 和调试
DalvikVM[localhost:8631]
Thread [<1> main] (Suspended (exception NullPointerException))
DashboardActivity$MyLocationListener.onLocationChanged(Location) line: 75
LocationManager$ListenerTransport._handleMessage(Message) line: 191
LocationManager$ListenerTransport.access$000(LocationManager$ListenerTransport, Message) line: 124
LocationManager$ListenerTransport$1.handleMessage(Message) line: 140
LocationManager$ListenerTransport$1(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626
NativeStart.main(String[]) line: not available [native method]
Thread [<6> Binder Thread #2] (Running)
Thread [<5> Binder Thread #1] (Running)
Thread [<7> MapService] (Running)
Thread [<8> TrafficService] (Running)
Daemon Thread [<10> RefQueueWorker@org.apache.http.impl.conn.tsccm.ConnPoolByRoute@4610b970] (Running)
第 75 行给出错误。
【问题讨论】:
-
你能发布你的 android-manifest.xml 吗?我想你忘了给它添加位置权限。
-
@Marcelo 只是好奇,我也认为权限是罪魁祸首。但根据文档 (developer.android.com/reference/android/location/…),如果“不存在合适的权限”,该方法会抛出
SecurityException。如果直接在事件线程中运行,这个异常不应该出现在 logcat 中甚至抛出 ANR 吗? -
我已经写了所有这些权限:
-
@MisterSmith 是的,它应该使用
SecurityException强制关闭应用程序,我错过了应用程序可以在模拟器上获取位置的部分,而问题仅在真实设备上。跨度>
标签: android google-maps geolocation gps location