【问题标题】:GooglePlayServicesUtil.getErrorDialog is nullGooglePlayServicesUtil.getErrorDialog 为空
【发布时间】:2012-12-05 15:07:05
【问题描述】:

我正在使用 ACRA (arca.ch) 生成自动错误报告。

我刚刚使用 Google Maps Android API v2 发布了我的应用程序的新版本。我在尝试显示 GooglePlayServicesUtil.getErrorDialog 返回的对话框时收到 EEEPad 和 Transformer Pad 用户报告的错误。有谁知道为什么会发生这种情况?

这是acra报告的相关代码和Logcat:

调用此行时:

int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(resultCode != ConnectionResult.SUCCESS)
{
        //The dialog that comes back is null (probably due to the logcat message)
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, 69);
        //So when I call the next line, the app crashes with a NullPointerException
        dialog.show();
}
...

Logcat:

12-18 04:21:04.531 W/GooglePlayServicesUtil( 3977): Google Play Store signature invalid.
12-18 04:21:04.551 E/GooglePlayServicesUtil( 3977): Google Play services is invalid. Cannot recover.

提前感谢您提供的任何帮助。

更新

谷歌尚未解决该问题,我将在听到任何消息后更新此问题(请参阅 CommonsWare 对 Google 错误报告链接的回答)。同时,如果您遇到此问题并且不希望您的应用崩溃,我目前正在做的是:

public void checkGooglePlayServicesAvailability()
{
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if(resultCode != ConnectionResult.SUCCESS)
    {
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, 69);
        if(dialog != null)
        {
            dialog.show();                
        }
        else
        {
            showOkDialogWithText(this, "Something went wrong. Please make sure that you have the Play Store installed and that you are connected to the internet. Contact developer with details if this persists.");
        }
    }

    Log.d("GooglePlayServicesUtil Check", "Result is: " + resultCode);
}

public static void showOkDialogWithText(Context context, String messageText)
{
    Builder builder = new AlertDialog.Builder(context);
    builder.setMessage(messageText);
    builder.setCancelable(true);
    builder.setPositiveButton("OK", null);
    AlertDialog dialog = builder.create();
    dialog.show();
}

【问题讨论】:

  • 嗨 DiscDev,当我尝试使用 showOkDialogWithText() 时会抛出错误,提示“无法在未调用 looper.prepare() 的线程内创建处理程序”为什么它试图在工作线程上运行而不是 UI 线程?

标签: android google-maps google-maps-android-api-2 google-play-services


【解决方案1】:

如果结果代码为SERVICE_MISSING、SERVICE_VERSION_UPDATE_REQUIRED 或SERVICE_DISABLED,Google suggests(也在docs 中)调用getErrorDialog()。所以可能是最后一个可能的状态码 (SERVICE_INVALID) 造成了麻烦。

我正在使用以下代码,到目前为止它似乎工作正常(在模拟器中测试,平台 2.3.3):

int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity.getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS) {
    activity.selectMap();
} else if (resultCode == ConnectionResult.SERVICE_MISSING ||
           resultCode == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED ||
           resultCode == ConnectionResult.SERVICE_DISABLED) {
    Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, activity, 1);
    dialog.show();
}

【讨论】:

  • 我刚刚用ConnectionResult.SERVICE_INVALID 测试了getErrorDialog 并在我的Nexus S 上得到了null。所以我想这个答案是正确的。
  • 那么,您要一起忽略 SERVICE_INVALID 吗?难道不应该这样处理吗?
  • 应该使用 GoogleApiAvailability,而不是 GooglePlayServicesUtil。
【解决方案2】:

在尝试显示对话框之前,您似乎必须与isUserRecoverableError 核对。

  int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
  if (status != ConnectionResult.SUCCESS) {
    if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
      GooglePlayServicesUtil.getErrorDialog(status, this, 
      REQUEST_CODE_RECOVER_PLAY_SERVICES).show();
    } else {
      Toast.makeText(this, "This device is not supported.", 
          Toast.LENGTH_LONG).show();
      finish();
    }
  }

【讨论】:

  • +1 - GooglePlayServicesUtil.isUserRecoverableError(status) 比枚举所有可能的状态更优雅。
  • +1 - 我现在正在使用它,但是在阅读了这个问题之后,我想我也会检查 null,因为它很容易添加。
【解决方案3】:

基于 Rahim 的代码,我添加了防止用户关闭 Google Play 服务对话框(通过点击返回按钮)并在未安装 Google Play 服务的情况下继续使用应用程序的功能:

private void checkGooglePlayServicesAvailable() {
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (status != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, 0);
            dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    MainActivity.this.finish();
                }
            });
            dialog.show();
        } else {
            Toast.makeText(this, "This device is not supported.", Toast.LENGTH_LONG).show();
            finish();
        }
    }
}

【讨论】:

    【解决方案4】:

    @Nevermore 的答案更新,因为 GooglePlayServicesUtil 方法已被 GoogleApiAvailability 弃用:

    GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
    int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(activity.getApplicationContext());
    if (resultCode == ConnectionResult.SUCCESS) {
        activity.selectMap();
    } else if (resultCode == ConnectionResult.SERVICE_MISSING ||
               resultCode == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED ||
               resultCode == ConnectionResult.SERVICE_DISABLED) {
        Dialog dialog = googleApiAvailability.getErrorDialog(activity, resultCode, 1);
        dialog.show();
    }
    

    注意getErrorDialog() 中前两个参数的顺序在GoogleApiAvailability 实现中已被调换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多