当通过GPS查找位置时,应用程序强制关闭

时间:2011-04-14 02:05:37

标签: android eclipse error-handling logcat

嗨我每次运行我的应用程序时都会收到一个强制关闭..它允许启动闪屏,但是当我点击“查找我的位置”按钮时它会强制关闭....我发现的东西奇怪的是,当我的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());
        }
    }
}

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}

2 个答案:

答案 0 :(得分:1)

您说您拥有正确的权限。只是为了确定,您知道位置有两种不同的权限吗?

String ACCESS_COARSE_LOCATION允许应用程序访问粗略(例如,Cell-ID,WiFi)位置 字符串ACCESS_FINE_LOCATION允许应用程序访问精细(例如GPS)位置

Manifest.permission

答案 1 :(得分:0)

这很重要

EXCEPTION: main 04-14 02:41:42.782: ERROR/AndroidRuntime(13759): java.lang.NullPointerException 

ERROR/AndroidRuntime(13759):     at com.irwine.touristapplication.MyLocOverlay.draw(MyLocOverlay.java:23)

如果我不得不猜测,我会说你的位置是空的。尝试在调试模式下运行,看看你能找到什么。我的建议是在错误行之前设置一个断点并查看变量是什么..如果你的位置变量为null,那就是你的问题,解决这个问题的方法就是保持ping,直到你得到它为止。

解决方案是检查位置是否为空..如果它为null,则表示您需要再次请求该位置。如果您尝试ping 20s并且没有得到任何东西,请继续ping,直到获得某些东西。理想情况下,您希望它在5次尝试后超时...... 这是因为如果您经常ping通,用户会讨厌您。然而;如果100分过去并且您没有获得位置,则超时并让用户再次请求该位置或要求他们稍后再试。

<强>更新

具体来说,我看到了这个错误。以下是我将如何处理它:

申请地点 - &gt;通过位置监听器等待位置 - &gt;收到位置后,再更新叠加层。

只要返回的位置为空,就继续请求。像我说的那样超时。

以下是一个例子:

我的LocationListener类:

public class myLocationListener implements LocationListener {
        Location L;

        public void onLocationChanged(Location arg0) {
            Toast.makeText(getApplicationContext(), "Location has changed",
                    Toast.LENGTH_SHORT).show();
            L = arg0;
            updateNewLocation(L);
        }}

我打电话的功能:

public void updateNewLocation(Location loc) {

        // while the location is not null get latitude and longitude
        // information
        // :: Null means that the GPS provider is off or there is an issue
        if (loc != null) {}}

我用来请求位置以及超时的函数体:

Toast.makeText(getApplicationContext(),
                            "Searching for your location.", Toast.LENGTH_SHORT)
                            .show();
                    locationManager.requestLocationUpdates(provider, 20000, 5,
                            locationListener);

                    // Thread to count until timeout (15 * 5000ms)
                    Thread t = new Thread(new Runnable() {
                        public void run() {
                            count = 0;

                            for (int i = 0; i < 15; i++) {
                                if (locationListener.L == null) {
                                    try {
                                        handler.sendMessage(handler
                                                .obtainMessage());
                                        Thread.sleep(5000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }

                            }

帮助处理超时的处理程序

Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            count++;
            if (count == 15) {
                error();
                count = 0;
            }
        }

    };