我的应用使用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");
}
答案 0 :(得分:0)
DeadObjectException - 你正在调用的对象已经死亡,因为它的托管进程已经不存在了。那就是服务已经停止 - 无论是从操作系统中死掉,还是从你的应用程序中停止。
重写onDestroy()方法并观察哪些事件导致DeadObjectException。如果您在不经过此方法的情况下捕获DeadObjectException
,则您的服务应该已被操作系统杀死。
答案 1 :(得分:0)
在onLocationChanged(location)中,saveLocation(location)使用RealmIO来保存用户的最后位置。
如果我删除saveLocation(位置),一切运行良好。
我发现如果我使用(RealmIO)或(与Gson的SharedPreferences)来保存数据,则会出现问题
临时解决方案是使用不带Gson的SharedPreferences:
Calling function func
I'm in A
我不知道为什么崩溃恰好发生在某些特定设备上。希望这有助于某人!