問(wèn)題描述
我有一個(gè)在我的應(yīng)用程序中實(shí)現(xiàn) LocationListener 的活動(dòng),并且我的 onLocationChanged 方法直到最近一直運(yùn)行良好.由于某種原因,傳遞給該方法的 Location 對(duì)象為空.我的問(wèn)題是,為什么它是空的?如果無(wú)法獲取位置,是否傳入一個(gè)空對(duì)象?
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 已打開(kāi),我已將其設(shè)置為從 GPS 中提取位置,并且我知道我的 GPS 可以在我使用地圖時(shí)找到我并讓它定位我.
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.
另外,我的應(yīng)用權(quán)限設(shè)置為:
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" />
但我不認(rèn)為權(quán)限是問(wèn)題,因?yàn)槲业幕顒?dòng)是事先進(jìn)行的,并且從那時(shí)起我沒(méi)有更改權(quán)限.
I don't believe permissions is the issue though, as my activity was working beforehand and I have not altered the permissions since then.
這里是確定位置對(duì)象提供者的代碼:
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));
這是錯(cuò)誤的 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]
我已經(jīng)在另一臺(tái)設(shè)備(確切地說(shuō)是 HTC MyTouch 3G)上測(cè)試了相同的代碼,它與 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.
推薦答案
我已經(jīng)解決了這個(gè)問(wèn)題,但我不確定為什么會(huì)這樣.
I have fixed the problem, though I'm not sure why this worked.
我所做的是將我的活動(dòng)設(shè)置為向最佳位置提供者(在本例中為 GPS)請(qǐng)求位置更新,并將其設(shè)置為在其位置精確到 50 米時(shí)觸發(fā)我的功能.我之前所做的只是嘗試檢索最后一個(gè)已知位置,但現(xiàn)在我將其設(shè)置為等到 GPS 可以得到修復(fù).
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.
也許它沒(méi)有存儲(chǔ)我最后一個(gè)已知位置,因此返回 null.
Perhaps it wasn't storing my last known location and thus was returning null.
這篇關(guān)于傳遞給 onLocationChanged 的??位置對(duì)象為空?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!