From c7ffef698bef8679cb1414cd30ae6517b1fb3dc6 Mon Sep 17 00:00:00 2001 From: hughchen Date: Wed, 20 Jun 2018 17:25:55 +0800 Subject: [PATCH] Add NPE check when showing error dialog. Add NPE check when showing error dialog Bug: 110263996 Test: make -j42 RunSettingsRoboTests ROBOTEST_FILTER=UtilsTest Change-Id: I93d6c662ea7031a631ac8c2c33cacc974677c3d7 --- src/com/android/settings/bluetooth/Utils.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/com/android/settings/bluetooth/Utils.java b/src/com/android/settings/bluetooth/Utils.java index 40864b3a4af..96f5da5b298 100755 --- a/src/com/android/settings/bluetooth/Utils.java +++ b/src/com/android/settings/bluetooth/Utils.java @@ -22,6 +22,7 @@ import android.bluetooth.BluetoothProfile; import android.content.Context; import android.content.DialogInterface; import android.provider.Settings; +import android.util.Log; import android.widget.Toast; import com.android.internal.logging.nano.MetricsProto.MetricsEvent; @@ -40,6 +41,9 @@ import androidx.annotation.VisibleForTesting; * for creating dialogs. */ public final class Utils { + + private static final String TAG = "BluetoothUtils"; + static final boolean V = BluetoothUtils.V; // verbose logging static final boolean D = BluetoothUtils.D; // regular logging @@ -107,11 +111,15 @@ public final class Utils { String message = context.getString(messageResId, name); Context activity = manager.getForegroundActivity(); if (manager.isForegroundActivity()) { - new AlertDialog.Builder(activity) - .setTitle(R.string.bluetooth_error_title) - .setMessage(message) - .setPositiveButton(android.R.string.ok, null) - .show(); + try { + new AlertDialog.Builder(activity) + .setTitle(R.string.bluetooth_error_title) + .setMessage(message) + .setPositiveButton(android.R.string.ok, null) + .show(); + } catch (Exception e) { + Log.e(TAG, "Cannot show error dialog.", e); + } } else { Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); }