java - Android Google API 客户端未连接

标签 java android google-api-client

大家好,我遇到了一个问题,当我创建一个新的 Google Api 客户端实例并调用方法“.connect”时,我收到一个异常,提示 GoogleApiClient 尚未连接。

主 Activity .java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.googleApiClient = new GoogleApiClient.Builder(this)
        .addApi(LocationServices.API)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();

    //Setup LocationRequest
    this.locationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(1000 * 10)
            .setInterval(1000 * 3);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
@Override
public void onStart(){
    super.onStart();
    this.googleApiClient.connect();
}

@Override
public void onStop(){
    super.onStop();
    this.googleApiClient.disconnect();
}

异常(exception):

08-27 22:54:04.536 11610-11610/at.ideafactory.spotted E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: at.ideafactory.spotted, PID: 11610
                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{at.ideafactory.spotted/at.ideafactory.spotted.Activity.Main.MainActivity}: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:156)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:157)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5872)
                                                                        at java.lang.reflect.Method.invokeNative(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:515)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
                                                                        at dalvik.system.NativeStart.main(Native Method)
                                                                     Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
                                                                        at com.google.android.gms.internal.zzpy.zzd(Unknown Source)
                                                                        at com.google.android.gms.location.internal.zzd.requestLocationUpdates(Unknown Source)
                                                                        at at.ideafactory.spotted.Activity.Main.MainActivity.onCreate(MainActivity.java:108)
                                                                        at android.app.Activity.performCreate(Activity.java:5312)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653) 
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:156) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:157) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5872) 
                                                                        at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                        at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674) 
                                                                        at dalvik.system.NativeStart.main(Native Method) 

也许任何人都可以帮我解决这个问题。

亲切的问候

最佳答案

这是因为您试图在 onStart 中使用 connect()。在您的 onStart 和 onStop 方法中放置一个空检查,如下所示

@Override
public void onStart(){
    super.onStart();
    if(this.googleApiClient != null){
            this.googleApiClient.connect();
    }
}

类似地在 onStop() 中放置一个空检查。

编辑

您应该按如下方式进行。首先在您的 Activity 中实现这些。在 OnConnected 回调中执行以下操作。

public class MainActivity extends AppCompatActivity implements
    GoogleApiClient.OnConnectionFailedListener,
    GoogleApiClient.ConnectionCallbacks,
    LocationListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buildGoogleApiClient();
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Log.d(TAG, "Connection established. Fetching location ..");
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

    @Override
    public void onLocationChanged(Location location) {

        Log.d("Lat: " + location.getLatitude() + "Lng : " + location.getLongitude());    
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    }

    public synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(getBaseContext())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
        mGoogleApiClient.connect();
    }

    @Override
    public void onPause() {
        super.onPause();
        //stop location updates when Activity is no longer active
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }
    }
}

关于java - Android Google API 客户端未连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39185723/

相关文章:

java - 我是否仍然应该在生产代码中使用 JAXB 对象中的枚举?

android - 如何检测字符按键?

php - 谷歌 API - 没有 refresh_token

java - 代码行解释

android - 使用 GoogleApiClient + LocationServices 不更新

java - Sonarqube Scanner CLI是否支持插件?

java - 如何从数组列表中读取多个不带符号的整数?

Java 优先级

java - 来自 ACTION_IMAGE_CAPTURE 的图像已旋转

android - 如何使用我的 android 应用程序在谷歌驱动器上上传文件