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/AndroidR开发者_StackOverflow社区untime(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.
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.
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).
I figured it out at last . there;s no problem what so ever in the code .
JUST SIMPLY RUN AN APPLICATION THAT WOULD STORE THE LATITUDE AND LONGITUDE DETAILS ...SAY LIKE YOUR MAPS BUILT IN APPLICATION IN YOUR MOBILE AND THEN RUN IT IT WOULD WORK FINE...
the reason why it gives a null is your phone would have stored the last known details and stack for it would be empty hence it pass a null . Running an app similar to maps or so would place in the location details in the stack and would fetch them back .. cheeers! @srinivas
精彩评论