GMS IllegalStateException:结果已经设置好了吗?

时间:2017-12-09 06:50:31

标签: android google-play-services illegalstateexception uncaughtexceptionhandler

从上周开始,我们的应用程序出现了很多例外情况。我们使用GMS 11.0.2

Fatal Exception: java.lang.IllegalStateException: Results have already been set
   at com.google.android.gms.common.internal.zzbo.zza(Unknown Source)
   at com.google.android.gms.internal.zzbbl.setResult(Unknown Source)
   at com.google.android.gms.internal.zzbbf.zzz(Unknown Source)
   at com.google.android.gms.internal.zzbbf.zzf(Unknown Source)
   at com.google.android.gms.internal.zzbbf.zzb(Unknown Source)
   at com.google.android.gms.internal.zzbav.zza(Unknown Source:3)
   at com.google.android.gms.internal.zzbdk.zzb(Unknown Source)
   at com.google.android.gms.internal.zzbdk.zzrR(Unknown Source)
   at com.google.android.gms.internal.zzbdk.onConnected(Unknown Source)
   at com.google.android.gms.common.internal.zzaa.onConnected(Unknown Source)
   at com.google.android.gms.common.internal.zzn.zzsR(Unknown Source:2)
   at com.google.android.gms.common.internal.zze.zzy(Unknown Source)
   at com.google.android.gms.common.internal.zzh.handleMessage(Unknown Source:4)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:148)
   at android.os.HandlerThread.run(HandlerThread.java:61)

织物的日志报告。

我们使用firebase和gms版本是11.0.2

我们如何检查这些问题?

和gms版本11.8.0中的相同问题

java.lang.IllegalStateException: Results have already been set

   at com.google.android.gms.common.internal.zzbs.zza(Unknown Source)
   at com.google.android.gms.common.api.internal.zzs.setResult(Unknown Source)
   at com.google.android.gms.common.api.internal.zzm.zzy(Unknown Source)
   at com.google.android.gms.common.api.internal.zzm.zzf(Unknown Source)
   at com.google.android.gms.common.api.internal.zzm.zzb(Unknown Source)
   at com.google.android.gms.common.api.internal.zzc.zza(Unknown Source:3)
   at com.google.android.gms.common.api.internal.zzbr.zzb(Unknown Source)
   at com.google.android.gms.common.api.internal.zzbr.zzakz(Unknown Source)
   at com.google.android.gms.common.api.internal.zzbr.onConnected(Unknown Source)

3 个答案:

答案 0 :(得分:5)

这个黑客是基于Jamin和divonas的回答。它适用于Crashlytics,没有Crashlytics。在Application onCreate()方法中调用此方法。如果您使用的是Crashlytics,请在Crashlytics初始化后调用此方法。顺便说一句,ui线程ID可能不总是1。

/**
 * Hack for gms bug https://issuetracker.google.com/issues/70416429
 * https://stackoverflow.com/questions/47726111/gms-illegalstateexception-results-have-already-been-set
 */
private void handleGMS70416429() {
    final Thread.UncaughtExceptionHandler defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
    final long uiThreadId = Thread.currentThread().getId();
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            if (e != null && t.getId() != uiThreadId && e.getStackTrace() != null && e.getStackTrace().length > 0
                    && e.getStackTrace()[0].toString().contains("com.google.android.gms")
                    && e.getMessage() != null && e.getMessage().contains("Results have already been set")) {
                return; // non-UI thread
            }
            if (defaultExceptionHandler != null)
                defaultExceptionHandler.uncaughtException(t, e);
        }

    });
}

答案 1 :(得分:3)

由于该bug还没有修复,请在BaseApplication的onCreate()方法中调用handleGMSException()或实现自己的ExceptionHandler。这个黑客是基于Jamin的回答,更新了Hexise的评论。

private void handleGMSException() {
    Thread.UncaughtExceptionHandler rootHandler = Thread.getDefaultUncaughtExceptionHandler();
    Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {
        if (!isGMSException(thread, throwable)) {
            rootHandler.uncaughtException(thread, throwable);
        }
    });
}

private boolean isGMSException(Thread thread, Throwable throwable) {
    //Check if Main Thread.
    if (throwable == null || thread.getId() == 1) return false;

    if (throwable.getStackTrace() != null && throwable.getStackTrace().length > 0
            && throwable.getStackTrace()[0].toString().contains("com.google.android.gms")
            && throwable.getMessage().contains("Results have already been set")) {
        return true;
    }

    return false;
}

答案 2 :(得分:0)

我还没有解决这个问题,但是我试图通过UncaughtExceptionHandler抓住它。我使用了fabric,所以我在fabric之后注册了MyUncaughtExceptionHandler,这样我就可以确定是否先处理这个问题。如果我发现是这个例外。我会抓住它。

//try to catch some uncaught exception
 public static boolean crashInterceptor(Thread thread, Throwable throwable) {

if (throwable == null || thread.getId() == 1) {
  //Don't intercept the Exception of Main Thread.
  return false;
}

String classpath = null;
if (throwable.getStackTrace() != null && throwable.getStackTrace().length > 0) {
  classpath = throwable.getStackTrace()[0].toString();
}

//intercept GMS Exception
if (classpath != null
    && throwable.getMessage().contains("Results have already been set")
    && classpath.contains("com.google.android.gms")) {
  //CrashHelper.logNonFatalException();
  return true;
}

return false;
}
}