GooglePlayServicesUtil.getErrorDialog имеет значение null
Я использую ACRA (arca.ch), чтобы генерировать автоматические отчеты об ошибках.
Я только что выпустил новую версию своего приложения, используя Google Maps Android API v2. Я получаю сообщение об ошибках пользователей EEEPad и Transformer Pad при попытке показать диалог, возвращенный GooglePlayServicesUtil.getErrorDialog. Кто-нибудь знает, почему это может случиться?
Вот соответствующий код и Logcat, как сообщает acra:
При вызове этой строки:
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.
Заранее благодарим за любую помощь, которую вы можете предоставить.
Обновление
Проблема еще не разрешена Google, и я обновлю этот вопрос, когда что-нибудь услышу (см. ответ 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();
}
Ответы
Ответ 1
Google предлагает (также в docs), вызывающий getErrorDialog()
, если код результата SERVICE_MISSING
, SERVICE_VERSION_UPDATE_REQUIRED
или SERVICE_DISABLED
. Возможно, что последний возможный код состояния (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();
}
Ответ 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();
}
}
Ответ 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
.