通过 GPS 查找位置时应用程序强制关闭
嗨,每次运行我的应用程序时,我都会强制关闭..它允许出现闪屏,但是一旦我单击“查找我的位置”按钮,它就会强制关闭....我发现奇怪的是当我的 GPS 关闭时,Wi-Fi 工作正常。我已经检查以确保我在清单中也拥有正确的权限。我已附上 logcat。我是否假设错误出现在第 23 行的 MyLocOverlay 类中?有人可以指出我解决此问题的正确方向吗? 谢谢
public class MyLocOverlay extends Overlay {
private final int Radius = 5;
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
if (shadow == false) {
// line below is line 23
Double lat = location.getLatitude()*1E6;
Double lng = location.getLongitude()*1E6;
GeoPoint geoPoint; geoPoint = new GeoPoint(lat.intValue(),lng.intValue());
}
}
}
日志猫
04-14 02:41:30.332: INFO/ActivityManager(102): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.irwine.touristapplication/.SplashScreen }
04-14 02:41:30.502: INFO/ActivityManager(102): Start proc com.irwine.touristapplication for activity com.irwine.touristapplication/.SplashScreen: pid=13759 uid=10060 gids={3003}
04-14 02:41:31.012: WARN/Rosie(5927): mAddHtcWidgetByOtherActivity = false, mIsOpenSlideWhenLeaveLaunch = true
04-14 02:41:31.012: WARN/InputManagerService(102): Ignoring hideSoftInput of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44b033b8
04-14 02:41:31.132: WARN/InputManagerService(102): Got RemoteException sending setActive(false) notification to pid 13731 uid 10060
04-14 02:41:31.322: INFO/ActivityManager(102): Displayed activity com.irwine.touristapplication/.SplashScreen: 834 ms (total 33041 ms)
04-14 02:41:36.152: INFO/ActivityManager(102): Starting activity: Intent { cmp=com.irwine.touristapplication/.mainmenu }
04-14 02:41:36.542: INFO/ActivityManager(102): Displayed activity com.irwine.touristapplication/.mainmenu: 337 ms (total 337 ms)
04-14 02:41:40.152: INFO/ActivityManager(102): Starting activity: Intent { cmp=com.irwine.touristapplication/.gps }
04-14 02:41:42.502: INFO/MapActivity(13759): Handling network change notification:CONNECTED
04-14 02:41:42.502: ERROR/MapActivity(13759): Couldn't get connection factory client
04-14 02:41:42.752: WARN/dalvikvm(13759): threadid=1: thread exiting with uncaught exception (group=0x40028a00)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): FATAL EXCEPTION: main
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): java.lang.NullPointerException
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at com.irwine.touristapplication.MyLocOverlay.draw(MyLocOverlay.java:23)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at com.google.android.maps.Overlay.draw(Overlay.java:179)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:45)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at com.google.android.maps.MapView.onDraw(MapView.java:494)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.View.draw(View.java:6739)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.View.draw(View.java:6742)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.widget.FrameLayout.draw(FrameLayout.java:352)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.View.draw(View.java:6742)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.widget.FrameLayout.draw(FrameLayout.java:352)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1872)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewRoot.draw(ViewRoot.java:1422)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewRoot.performTraversals(ViewRoot.java:1167)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.os.Looper.loop(Looper.java:143)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.app.ActivityThread.main(ActivityThread.java:4914)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at java.lang.reflect.Method.invoke(Method.java:521)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at dalvik.system.NativeStart.main(Native Method)
04-14 02:41:42.802: WARN/ActivityManager(102): Force finishing activity com.irwine.touristapplication/.gps
04-14 02:41:42.812: WARN/ActivityManager(102): Force finishing activity com.irwine.touristapplication/.mainmenu
04-14
02:41:43.316: WARN/ActivityManager(102): Activity pause timeout for HistoryRecord{44eb0130 com.irwine.touristapplication/.gps}
04-14 02:41:44.742: INFO/Process(13759): Sending signal. PID: 13759 SIG: 9
04-14 02:41:44.752: INFO/WindowManager(102): WIN DEATH: Window{44b42110 com.irwine.touristapplication/com.irwine.touristapplication.mainmenu paused=true}
04-14 02:41:44.762: INFO/ActivityManager(102): Process com.irwine.touristapplication (pid 13759) has died.
04-14 02:41:44.772: INFO/WindowManager(102): WIN DEATH: Window{44b66298 com.irwine.touristapplication/com.irwine.touristapplication.gps paused=false}
04-14 02:41:53.654: WARN/ActivityManager(102): Activity destroy timeout for HistoryRecord{44bd91e8 com.irwine.touristapplication/.mainmenu}
04-14 02:41:53.699: WARN/ActivityManager(102): Activity destroy timeout for HistoryRecord{44eb0130 com.irwine.touristapplication/.gps}
Hi i am getting a force close everytime i run my application.. it allows the splashscreen to appear, but as soon as i click on 'Find My Location' button it forces close.... the thing i'm finding strange is that is works fine with wi-fi when my gps is turned off.. I have checked to ensure the i have the right permissions in the manifest also. I've attached the logcat. Am i assuming right by this the error is in MyLocOverlay class on line 23? Could someone please point me in the right direction for fixing this?
Thanks
public class MyLocOverlay extends Overlay {
private final int Radius = 5;
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
if (shadow == false) {
// line below is line 23
Double lat = location.getLatitude()*1E6;
Double lng = location.getLongitude()*1E6;
GeoPoint geoPoint; geoPoint = new GeoPoint(lat.intValue(),lng.intValue());
}
}
}
Logcat
04-14 02:41:30.332: INFO/ActivityManager(102): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.irwine.touristapplication/.SplashScreen }
04-14 02:41:30.502: INFO/ActivityManager(102): Start proc com.irwine.touristapplication for activity com.irwine.touristapplication/.SplashScreen: pid=13759 uid=10060 gids={3003}
04-14 02:41:31.012: WARN/Rosie(5927): mAddHtcWidgetByOtherActivity = false, mIsOpenSlideWhenLeaveLaunch = true
04-14 02:41:31.012: WARN/InputManagerService(102): Ignoring hideSoftInput of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44b033b8
04-14 02:41:31.132: WARN/InputManagerService(102): Got RemoteException sending setActive(false) notification to pid 13731 uid 10060
04-14 02:41:31.322: INFO/ActivityManager(102): Displayed activity com.irwine.touristapplication/.SplashScreen: 834 ms (total 33041 ms)
04-14 02:41:36.152: INFO/ActivityManager(102): Starting activity: Intent { cmp=com.irwine.touristapplication/.mainmenu }
04-14 02:41:36.542: INFO/ActivityManager(102): Displayed activity com.irwine.touristapplication/.mainmenu: 337 ms (total 337 ms)
04-14 02:41:40.152: INFO/ActivityManager(102): Starting activity: Intent { cmp=com.irwine.touristapplication/.gps }
04-14 02:41:42.502: INFO/MapActivity(13759): Handling network change notification:CONNECTED
04-14 02:41:42.502: ERROR/MapActivity(13759): Couldn't get connection factory client
04-14 02:41:42.752: WARN/dalvikvm(13759): threadid=1: thread exiting with uncaught exception (group=0x40028a00)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): FATAL EXCEPTION: main
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): java.lang.NullPointerException
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at com.irwine.touristapplication.MyLocOverlay.draw(MyLocOverlay.java:23)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at com.google.android.maps.Overlay.draw(Overlay.java:179)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:45)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at com.google.android.maps.MapView.onDraw(MapView.java:494)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.View.draw(View.java:6739)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.View.draw(View.java:6742)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.widget.FrameLayout.draw(FrameLayout.java:352)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.View.draw(View.java:6742)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.widget.FrameLayout.draw(FrameLayout.java:352)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1872)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewRoot.draw(ViewRoot.java:1422)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewRoot.performTraversals(ViewRoot.java:1167)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.os.Looper.loop(Looper.java:143)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at android.app.ActivityThread.main(ActivityThread.java:4914)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at java.lang.reflect.Method.invoke(Method.java:521)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-14 02:41:42.782: ERROR/AndroidRuntime(13759): at dalvik.system.NativeStart.main(Native Method)
04-14 02:41:42.802: WARN/ActivityManager(102): Force finishing activity com.irwine.touristapplication/.gps
04-14 02:41:42.812: WARN/ActivityManager(102): Force finishing activity com.irwine.touristapplication/.mainmenu
04-14
02:41:43.316: WARN/ActivityManager(102): Activity pause timeout for HistoryRecord{44eb0130 com.irwine.touristapplication/.gps}
04-14 02:41:44.742: INFO/Process(13759): Sending signal. PID: 13759 SIG: 9
04-14 02:41:44.752: INFO/WindowManager(102): WIN DEATH: Window{44b42110 com.irwine.touristapplication/com.irwine.touristapplication.mainmenu paused=true}
04-14 02:41:44.762: INFO/ActivityManager(102): Process com.irwine.touristapplication (pid 13759) has died.
04-14 02:41:44.772: INFO/WindowManager(102): WIN DEATH: Window{44b66298 com.irwine.touristapplication/com.irwine.touristapplication.gps paused=false}
04-14 02:41:53.654: WARN/ActivityManager(102): Activity destroy timeout for HistoryRecord{44bd91e8 com.irwine.touristapplication/.mainmenu}
04-14 02:41:53.699: WARN/ActivityManager(102): Activity destroy timeout for HistoryRecord{44eb0130 com.irwine.touristapplication/.gps}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你说你有正确的权限。可以确定的是,您知道位置有两种不同的权限吗?
字符串 ACCESS_COARSE_LOCATION 允许应用程序访问粗略(例如,Cell-ID、WiFi)位置
String ACCESS_FINE_LOCATION 允许应用程序访问精细(例如 GPS)位置
Manifest.permission
You say you have the right permissions. Just to be sure, you know that there are two different permissions for location?
String ACCESS_COARSE_LOCATION Allows an application to access coarse (e.g., Cell-ID, WiFi) location
String ACCESS_FINE_LOCATION Allows an application to access fine (e.g., GPS) location
Manifest.permission
这很重要
如果我必须猜测,我会说您的位置为空。尝试在调试模式下运行,看看能找到什么。我的建议是在错误行之前放置一个断点,然后查看变量是什么。如果您的位置变量为空,那就是您的问题,解决此问题的方法是继续 ping 直到得到它。
解决方案是检查位置是否为空。如果为空,则意味着您需要再次请求位置。如果您尝试 ping 20 秒但没有得到任何结果,请继续 ping 直到得到结果。理想情况下,您希望它在 5 次尝试后超时...这是因为如果您不断 ping,用户会讨厌您。然而;如果 100 秒过去了而您没有获得位置,则超时并让用户再次请求该位置或要求他们稍后重试。
更新
具体来说,我发现了一个错误。以下是我的处理方法:
请求位置 ->通过位置监听器等待位置->收到位置后,更新叠加层。
只要返回的位置为空,就继续请求。就像我说的那样超时。
这是一个示例:
我的 LocationListener 类:
我调用的函数:
我用来请求位置和超时的函数体:
帮助处理超时的处理程序
This is important
if I had to guess, I'd say that your location is null. Try running in debug mode and see what you can find. My suggestion is put a break point BEFORE the error line and look at what the variables are.. if your location variable is null, that is your problem, way to fix this is to keep pinging until you get it..
The solution is to check if the location is null.. if its null, then this means you need to request the location again. If you try pinging for 20s and don't get anything, keep pinging until you get something. Ideally you want it to time out after 5 tries... This is because if you ping constantly the user will hate you. However; If 100s pass and you don't get a location, then time out and have the user request that location again or ask them to try again later.
UPDATE
Specifically, i see an error with this. Here is how I would approach it:
Request the location -> Wait for the location through the location listener -> Once the locations received then update the overlay.
Keep requesting as long as the location returned is null. Time out like I said.
Here is an example:
my LocationListener class:
Function I call:
Function body I use to request location along with timeout:
Handler to help with timeout