无法访问Google API客户端?

时间:2017-05-13 07:03:43

标签: java android string api google-maps

我已在我的项目中实施GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener(以及它的三个回调方法onConnectedonConnectionSuspended和& onConnectionFailed), 但我无法引用Google Location API客户端!

enter image description here(c只是一个保存上下文的对象)

我已尝试将参数更改为thisvolumeContentObserver.this,甚至事先在变量中保留上下文并在API客户端的参数中引用它。

这些选项都不起作用。我如何正确引用我的GoogleApiClient?

P.S。,这是完整的类声明:

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
public class volumeContentObserver extends ContentObserver implements LocationListener,
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

以下是问题代码:

 if (mGoogleApiClient == null) {
                                mGoogleApiClient = new GoogleApiClient.Builder(c)
                                        .addConnectionCallbacks(volumeContentObserver.this)
                                        .addOnConnectionFailedListener(volumeContentObserver.this)
                                        .addApi(LocationServices.API)
                                        .build()
                                        .connect();// Connecting the client
                            }

3 个答案:

答案 0 :(得分:0)

您正在分配连接响应,如果您想要连接,则返回void到mConnectApiClient:

   if (mGoogleApiClient == null) {
    mGoogleApiClient = new GoogleApiClient.Builder(c)
    .addConnectionCallbacks(volumeContentObserver.this)                                              .addOnConnectionFailedListener(volumeContentObserver.this)
    .addApi(LocationServices.API).build();
    mGoogleApiClient.connect()
    }

答案 1 :(得分:0)

问题是您在结尾处调用了connect(),而void不是GoogleApiClient的实例。

试试这个:

mGoogleApiClient = new GoogleApiClient.Builder(c)
                           .addConnectionCallbacks(volumeContentObserver.this)
                           .addOnConnectionFailedListener(volumeContentObserver.this)
                           .addApi(LocationServices.API)
                           .build();                                        
mGoogleApiClient.connect();

答案 2 :(得分:0)

正如我所做的那样,你应该试试这段代码,希望这项工作适合你

if (mGoogleApiClient == null) {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(LocationServices.API)
        .build();
}

在此之前你必须定义gooleAiClient.connect();在onStart metod。