【问题标题】:requestLocationUpdates() in LocationUpdate service gives null pointer errorLocationUpdate 服务中的 requestLocationUpdates() 给出空指针错误
【发布时间】:2019-06-18 04:38:49
【问题描述】:

应用程序在启动时崩溃并出现空指针异常。当已经授予位置权限时,就会发生这种情况。但是,在未授予位置权限且仅在应用启动后才授予位置权限时不会发生。

public void requestLocationUpdates() {
    Log.i(TAG, "Requesting location updates");
    Utils.setRequestingLocationUpdates(this, true);
    startService(new Intent(getApplicationContext(), 
LocationUpdatesService.class));
    try {
        mFusedLocationClient.requestLocationUpdates(mLocationRequest,
                mLocationCallback, Looper.myLooper());
    } catch (SecurityException unlikely) {
        Utils.setRequestingLocationUpdates(this, false);
        Log.e(TAG, "Lost location permission. Could not request updates. " + 
unlikely);
    }
}

以下是错误。

 java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.projectu/com.example.projectu.MainActivity}: 
java.lang.NullPointerException: Attempt to invoke virtual method 'void 
com.example.projectu.LocationUpdatesService.requestLocationUpdates()' on a 
null object reference
    at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
    at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual 
method 'void 
com.example.projectu.LocationUpdatesService.requestLocationUpdates()' on a 
null object reference
    at com.example.projectu.MainActivity.onStart(MainActivity.java:265)
    at 
android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
    at android.app.Activity.performStart(Activity.java:7029)
    at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741)

【问题讨论】:

标签: android nullpointerexception android-location google-location-services


【解决方案1】:

您尚未初始化 mFusedLocationClient,并在 null 对象上调用其 mFusedLocationClient.requestLocationUpdates

【讨论】:

猜你喜欢
  • 2018-07-20
  • 2013-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多