开发者

App force closes when find for location by 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(1开发者_运维知识库3759):     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}


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


This is important

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)

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:

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);
        }}

Function I call:

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) {}}

Function body I use to request location along with timeout:

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 to help with timeout

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

    };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜