要求获得位置许可后,Phonegap应用程序崩溃

时间:2017-01-29 22:50:54

标签: android jquery cordova

当应用程序没有位置权限并且我打开它时,它会要求许可,在我给它许可后它会崩溃。如果它有权限,它可以顺利运行。

我的位置代码是这个。
if(navigator.geolocation){
  navigator.geolocation.getCurrentPosition(
  function(position){
    // on success
    loadMap(position.coords.latitude, position.coords.longitude);
},
function(e){
  // on error
  loadMap(0,0);
}, {maximumAge: 30000, timeout: 5000, enableHighAccuracy: true})

loadMap函数传递参数,因此我可以创建一个mapbox map。

我正在使用phonegap 6.4.3和cordova 6.4.0并将其编译为带有Android 7.1的虚拟连接5

  

错误记录

这些是我点击“允许”后得到的错误消息
01-30 18:02:55.545   238   238 E SELinux : avc:  denied  { find } for service=batterymanager scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:battery_service:s0 tclass=service_manager
01-30 18:02:55.662 18458 18502 E libEGL  : validate_display:255 error 3008 (EGL_BAD_DISPLAY)
01-30 18:02:58.131 18458 18458 E cr_LocationProvider: Caught security exception while registering for location updates from the system. The application does not have sufficient geolocation permissions.
01-30 18:02:58.134 18458 18458 E cr_LocationProvider: Caught security exception while registering for location updates from the system. The application does not have sufficient geolocation permissions.
01-30 18:02:58.136 18458 18458 E cr_LocationProvider: Caught security exception while registering for location updates from the system. The application does not have sufficient geolocation permissions.
01-30 18:03:00.577 18458 18458 E AndroidRuntime: FATAL EXCEPTION: main
01-30 18:03:00.577 18458 18458 E AndroidRuntime: Process: com.phonegap.helloworld, PID: 18458
01-30 18:03:00.577 18458 18458 E AndroidRuntime: Theme: themes:{com.cyanogenmod.trebuchet=overlay:system}
01-30 18:03:00.577 18458 18458 E AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=0, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.phonegap.helloworld/com.phonegap.helloworld.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void org.apache.cordova.CallbackContext.sendPluginResult(org.apache.cordova.PluginResult)' on a null object reference
01-30 18:03:00.577 18458 18458 E AndroidRuntime:    at android.app.ActivityThread.deliverResults(ActivityThread.java:3733)
01-30 18:03:00.577 18458 18458 E AndroidRuntime:    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
01-30 18:03:00.577 18458 18458 E AndroidRuntime:    at android.app.ActivityThread.-wrap16(ActivityThread.java)
01-30 18:03:00.577 18458 18458 E AndroidRuntime:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
01-30 18:03:00.577 18458 18458 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:102)
01-30 18:03:00.577 18458 18458 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:148)
01-30 18:03:00.577 18458 18458 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:5461)
01-30 18:03:00.577 18458 18458 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)
01-30 18:03:00.577 18458 18458 E AndroidRuntime:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-30 18:03:00.577 18458 18458 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-30 18:03:00.577 18458 18458 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void org.apache.cordova.CallbackContext.sendPluginResult(org.apache.cordova.PluginResult)' on a null object reference
01-30 18:03:00.577 18458 18458 E AndroidRuntime:    at org.apache.cordova.geolocation.Geolocation.onRequestPermissionResult(Geolocation.java:77)
01-30 18:03:00.577 18458 18458 E AndroidRuntime:    at org.apache.cordova.CordovaInterfaceImpl.onRequestPermissionResult(CordovaInterfaceImpl.java:214)
01-30 18:03:00.577 18458 18458 E AndroidRuntime:    at org.apache.cordova.CordovaActivity.onRequestPermissionsResult(CordovaActivity.java:508)
01-30 18:03:00.577 18458 18458 E AndroidRuntime:    at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6582)
01-30 18:03:00.577 18458 18458 E AndroidRuntime:    at android.app.Activity.dispatchActivityResult(Activity.java:6460)
01-30 18:03:00.577 18458 18458 E AndroidRuntime:    at android.app.ActivityThread.deliverResults(ActivityThread.java:3729)
01-30 18:03:00.577 18458 18458 E AndroidRuntime:    ... 9 more

2 个答案:

答案 0 :(得分:0)

确保在页面中包含此行:

<script src="cordova.js"></script>

我遇到了同样的问题,然后意识到我没有包括cordova.js。

答案 1 :(得分:0)

问题可能是Geolocation插件在您使用时尚未就绪。 它应该在&#34; deviceready&#34;之后调用。事件

参考:cordova-plugin-geolocation