GoogleApiClient.connect()导致崩溃

问题描述 投票:1回答:2

我的应用程序使用GoogleApiClient来获取当前位置。它第一次运行良好但如果我关闭我的应用程序然后再次打开它,它会崩溃(在Sony Xperia Z3,Google Pixel上)。

在华硕,三星,......它不会崩溃

android.os.DeadObjectException
                                                   at android.os.BinderProxy.transactNative(Native Method)
                                                   at android.os.BinderProxy.transact(Binder.java:503)
                                                   at android.view.IWindow$Stub$Proxy.onAnimationStopped(IWindow.java:534)
                                                   at com.android.server.wm.WindowAnimator.updateWindowsLocked(WindowAnimator.java:286)
                                                   at com.android.server.wm.WindowAnimator.animateLocked(WindowAnimator.java:678)
                                                   at com.android.server.wm.WindowAnimator.access$000(WindowAnimator.java:53)
                                                   at com.android.server.wm.WindowAnimator$1.doFrame(WindowAnimator.java:123)
                                                   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:856)
                                                   at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                   at android.view.Choreographer.doFrame(Choreographer.java:603)
                                                   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                   at android.os.Looper.loop(Looper.java:234)
                                                   at android.os.HandlerThread.run(HandlerThread.java:61)
                                                   at com.android.server.ServiceThread.run(ServiceThread.java:46)

我发现导致崩溃的代码行是GoogleApiClient.connect(),我也使用try-catch,更新到最新的Google API 11.8.0但找不到可能的解决方案。这是我的代码

@Override
public void onLocationChanged(Location location) {
    if (location != null) {
        Log.v(">>>", "onLocationChanged Longitude = " + Constants.location.getLongitude() + ", Latitude = " + Constants.location.getLatitude());
        //Toast.makeText(this, "onLocationChanged Longitude = " + Constants.location.getLongitude() + ", Latitude = " + Constants.location.getLatitude(), Toast.LENGTH_SHORT).show();
        saveLocation(location);
    }
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
        try {
            Location mLastLocation = LocationServices.FusedLocationApi
                    .getLastLocation(mGoogleApiClient);
            if (mLastLocation != null) {...}

                //Toast.makeText(this, "onConnected Longitude = " + Constants.location.getLongitude() + ", Latitude = " + Constants.location.getLatitude(), Toast.LENGTH_SHORT).show();
            }
            startLocationUpdates();
        } catch (Exception ex) {
            // This will catch the exception, handle as needed
        }
    }
    //Toast.makeText(this, "onConnected", Toast.LENGTH_SHORT).show();
}

@Override
public void onConnectionSuspended(int i) {
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.d("Location service", "onConnectionFailed");
}

@Override
protected void onResume() {
    super.onResume();
    checkPlayServices();
    // Resuming the periodic location updates
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
        startLocationUpdates();
    }
    Log.v(">>>", "onResume");
}

@Override
protected void onStart() {
    super.onStart();
    if (mGoogleApiClient != null) {
        mGoogleApiClient.connect(); // CRASH HERE
        Log.v(">>>", "GoogleApiClient.connect()");
    }
    Log.v(">>>", "onStart");
}

@Override
protected void onStop() {
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
        mGoogleApiClient = null;
        Log.v(">>>", "GoogleApiClient.disconnect()");
    }
    super.onStop();
    Log.v(">>>", "onStop");
}

@Override
protected void onPause() {
    stopLocationUpdates();
    super.onPause();
    Log.v(">>>", "onPause");
}
android google-api-client
2个回答
0
投票

DeadObjectException - 你正在调用的对象已经死亡,因为它的托管进程已经不存在了。那就是服务已经停止 - 要么从操作系统中被杀死,要么从应用程序中停止。

覆盖你的onDestroy()方法并观察哪些事件导致DeadObjectException。如果你在没有经过这种方法的情况下捕获DeadObjectException,你的服务应该被操作系统杀死。


0
投票

在onLocationChanged(location)中,saveLocation(location)使用RealmIO来保存用户的最后位置。

如果我删除saveLocation(位置),一切都很好。

我发现如果我使用(RealmIO)或(与Gson的SharedPreferences)来保存数据,则会出现问题

临时解决方案是在没有Gson的情况下使用SharedPreferences:

SharedPreferences prefs = context.getSharedPreferences(
           LAST_LOCATION, Context.MODE_PRIVATE);
    prefs.edit().putString(LAST_LOCATION_LONGITUDE, location.getLongitude() + "").apply();
    prefs.edit().putString(LAST_LOCATION_LATITUDE, location.getLatitude() + "").apply();

我不知道为什么崩溃只发生在一些特定的设备上。希望这有帮助的人!

© www.soinside.com 2019 - 2024. All rights reserved.