GoogleApiClient参数是必需的

时间:2015-03-04 07:33:07

标签: android eclipse

我想获取设备的当前位置,这是我的一段代码

private void displayLocation() {

    mLastLocation = LocationServices.FusedLocationApi
            .getLastLocation(mGoogleApiClient);

    if (mLastLocation != null) {
        double latitude = mLastLocation.getLatitude();
        double longitude = mLastLocation.getLongitude();

        test1 = latitude;
        test2 = longitude;

    } else {
        double latitude = mLastLocation.getLatitude();
        double longitude = mLastLocation.getLongitude();
        test1 = latitude;
        test2 = longitude;
    }
}

我得到了这样的错误

03-04 07:24:33.500: E/AndroidRuntime(5427): FATAL EXCEPTION: main
 03-04 07:24:33.500: E/AndroidRuntime(5427): java.lang.RuntimeException: Unable to start activity         ComponentInfo{com.emildesign.navigationapplication/com.emildesign.navigationapplication.NavigationActivity}: java.lang.IllegalArgumentException: GoogleApiClient parameter is required.
03-04 07:24:33.500: E/AndroidRuntime(5427):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-04 07:24:33.500: E/AndroidRuntime(5427):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-04 07:24:33.500: E/AndroidRuntime(5427):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-04 07:24:33.500: E/AndroidRuntime(5427):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-04 07:24:33.500: E/AndroidRuntime(5427):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 07:24:33.500: E/AndroidRuntime(5427):     at android.os.Looper.loop(Looper.java:137)
03-04 07:24:33.500: E/AndroidRuntime(5427):     at android.app.ActivityThread.main(ActivityThread.java:4745)
03-04 07:24:33.500: E/AndroidRuntime(5427):     at java.lang.reflect.Method.invokeNative(Native Method)
03-04 07:24:33.500: E/AndroidRuntime(5427):     at java.lang.reflect.Method.invoke(Method.java:511)
03-04 07:24:33.500: E/AndroidRuntime(5427):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-04 07:24:33.500: E/AndroidRuntime(5427):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-04 07:24:33.500: E/AndroidRuntime(5427):     at dalvik.system.NativeStart.main(Native Method)
03-04 07:24:33.500: E/AndroidRuntime(5427): Caused by: java.lang.IllegalArgumentException: GoogleApiClient parameter is required.
03-04 07:24:33.500: E/AndroidRuntime(5427):     at com.google.android.gms.internal.jx.b(Unknown Source)
03-04 07:24:33.500: E/AndroidRuntime(5427):     at com.google.android.gms.location.LocationServices.f(Unknown Source)
03-04 07:24:33.500: E/AndroidRuntime(5427):     at com.google.android.gms.internal.nf.getLastLocation(Unknown Source)
03-04 07:24:33.500: E/AndroidRuntime(5427):     at com.emildesign.navigationapplication.NavigationActivity.displayLocation(NavigationActivity.java:167)
03-04 07:24:33.500: E/AndroidRuntime(5427):     at com.emildesign.navigationapplication.NavigationActivity.onCreate(NavigationActivity.java:51)
03-04 07:24:33.500: E/AndroidRuntime(5427):     at android.app.Activity.performCreate(Activity.java:5008)
03-04 07:24:33.500: E/AndroidRuntime(5427):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-04 07:24:33.500: E/AndroidRuntime(5427):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-04 07:24:33.500: E/AndroidRuntime(5427):     ... 11 more

它说java.lang.IllegalArgumentException:需要GoogleApiClient参数。 第167行是:mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您可以使用https://github.com/mcharmas/Android-ReactiveLocation在6行代码中获取最后已知的融合位置。

现在我无法看到您如何定义mGoogleApiClient,但您需要使用构建器创建它,例如:

    new GoogleApiClient.Builder(ctx)
   .apiClientBuilder.addApi(LocationServices.API)
   .addConnectionCallbacks(apiClientConnectionCallbacks)
   .addOnConnectionFailedListener(apiClientConnectionCallbacks).build();