0

嗨,我每次运行我的应用程序时都会强制关闭..它允许出现启动画面,但是一旦我点击“查找我的位置”按钮,它就会强制关闭....我发现奇怪的是当我的 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}
4

2 回答 2

1

你说你有正确的权限。可以肯定的是,您知道位置有两种不同的权限吗?

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

清单.permission

于 2011-04-14T02:48:19.707 回答
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)

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

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

更新

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

请求位置 -> 通过位置侦听器等待位置 -> 一旦收到位置,然后更新覆盖。

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

这是一个例子:

我的 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;
            }
        }

    };
于 2011-04-14T02:13:11.113 回答