传递给 onLocationChanged 的​​位置对象为空? [英] Location object passed into onLocationChanged is null?

查看:104
本文介绍了传递给 onLocationChanged 的​​位置对象为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在我的应用程序中实现 LocationListener 的活动,并且我的 onLocationChanged 方法直到最近一直运行良好.由于某种原因,传递给该方法的 Location 对象为空.我的问题是,为什么它是空的?如果无法获取位置,是否传入一个空对象?

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?

我的 GPS 已打开,我已将其设置为从 GPS 中提取位置,并且我知道我的 GPS 可以在我使用地图时找到我并让它定位我.

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

这是错误的 LogCat 输出:

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] 我已经在另一台设备(确切地说是 HTC MyTouch 3G)上测试了相同的代码,它与 GPS 完美配合,但是在我的 Motorola Droid 上它崩溃了.

[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.

我所做的是将我的活动设置为向最佳位置提供者(在本例中为 GPS)请求位置更新,并将其设置为在其位置精确到 50 米时触发我的功能.我之前所做的只是尝试检索最后一个已知位置,但现在我将其设置为等到 GPS 可以得到修复.

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.

也许它没有存储我最后一个已知位置,因此返回 null.

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

这篇关于传递给 onLocationChanged 的​​位置对象为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆