views:

147

answers:

3

I have an activity that implements LocationListener in my application and my onLocationChanged method has been working perfectly up until recently. For some reason the Location object being passed into the method is null. My question is, why is it null? Does it pass in a null object if it cannot acquire a location?

My GPS is on and I have it set to pull a location from the GPS, and I know my GPS can get a fix on me as I used Maps and had it locate me.

Also, I have the permission for my app set as:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

I don't believe permissions is the issue though, as my activity was working beforehand and I have not altered the permissions since then.

Here is the code that determines the provider of the location object:

String locationProvider = this.mLocationManager.getBestProvider(mLocationCriteria, true);
this.onLocationChanged(this.mLocationManager.getLastKnownLocation(locationProvider));

Here is the LogCat output of the error:

08-08 18:25:26.310: ERROR/MapActivity(6447): Couldn't get connection factory client
08-08 18:26:18.771: ERROR/AndroidRuntime(6447): Uncaught handler: thread main exiting due to uncaught exception
08-08 18:26:18.880: ERROR/AndroidRuntime(6447): java.lang.IllegalStateException: Could not execute method of the activity
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at android.view.View$1.onClick(View.java:2031)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at android.view.View.performClick(View.java:2364)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at android.view.View.onTouchEvent(View.java:4179)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at android.view.View.dispatchTouchEvent(View.java:3709)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at android.os.Looper.loop(Looper.java:123)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at android.app.ActivityThread.main(ActivityThread.java:4363)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at java.lang.reflect.Method.invokeNative(Native Method)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at java.lang.reflect.Method.invoke(Method.java:521)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at dalvik.system.NativeStart.main(Native Method)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447): Caused by: java.lang.reflect.InvocationTargetException
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at com.proto.screens.MyMapActivity.onLocateUserClick(MyMapActivity.java:147)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at java.lang.reflect.Method.invokeNative(Native Method)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at java.lang.reflect.Method.invoke(Method.java:521)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at android.view.View$1.onClick(View.java:2026)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     ... 21 more
08-08 18:26:18.880: ERROR/AndroidRuntime(6447): Caused by: java.lang.NullPointerException
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     at com.proto.screens.MyMapActivity.onLocationChanged(MyMapActivity.java:135)
08-08 18:26:18.880: ERROR/AndroidRuntime(6447):     ... 25 more

[UPDATE] I have tested the same code on another device (an HTC MyTouch 3G to be precise) and it works flawlessly with the GPS, however on my Motorola Droid it crashes.

A: 

I recommend you run this app using the debugger to find the cause of the null pointer. If the block of code that gets the location is in a try block you can add a breakpoint in the catch and the exception will give you a good idea what the cause is (and how to handle it).

JackN
I have run the debugger and can only tell that the provider I'm using (GPS) doesn't seem to be enabled...however from my settings (and from other apps that use GPS) I can see that it is enabled.I even changed my code to utilize the best enabled provider: String locationProvider = this.mLocationManager.getBestProvider(mLocationCriteria, true); this.onLocationChanged(this.mLocationManager.getLastKnownLocation(locationProvider));...but it still is returning null, which according to the documentation means that the provider is disabled.
celestialorb
A: 

Have you looked through your logcat for a pertinent error message?

gregS
Yes, and from what I can tell it's crashing because of the NullPointerException, which shouldn't be occurring if the GPS is activated and I have permission to utilize it.
celestialorb
Have you tried making the problem smaller until at last (one hopes) it becomes obvious? For example, you could split your compound statements defining locationProvider and onLocationChanged into multiple lines so that the line number reported in logcat is more helpful, you could try simulating the location in the emulator and see if the problem occurs with a certain SDK level, you could run backups of your code to determine if anything you've changed in your code has caused the NullPointerException.
gregS
The problem isn't with the retrieval of the Location provider though. It's with what the provider feeds back to me, it gives me a null location and that it what causes my NullPointerException (I try to manipulate the location).
celestialorb
A: 

I have fixed the problem, though I'm not sure why this worked.

What I did is I set my activity to request location updates from the best location provider (in this case the GPS) and set it to trigger my function onces it has a location accurate down to 50 meters. What I was doing before was just attempting to retrieve the last known location, but now I have it set to wait until the GPS can get a fix.

Perhaps it wasn't storing my last known location and thus was returning null.

celestialorb