From b04cce0eb5917db436b7db39eb67c064df2e015a Mon Sep 17 00:00:00 2001 From: Nick Pelly Date: Mon, 21 Nov 2011 17:02:02 -0800 Subject: [PATCH] Throw a nicer error message when using an invalid context to create Nfc This can happen when using getContext() instead of getTargetContext() in an InstrumentationTestCase. Bug: 5644274 Change-Id: I020a637c271e25bcf25ae2927fd878001d5fea0a --- core/java/android/nfc/NfcAdapter.java | 7 +++++-- core/java/android/nfc/NfcManager.java | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/core/java/android/nfc/NfcAdapter.java b/core/java/android/nfc/NfcAdapter.java index dec262bb31c5b..b6af0f2471a4f 100644 --- a/core/java/android/nfc/NfcAdapter.java +++ b/core/java/android/nfc/NfcAdapter.java @@ -357,8 +357,11 @@ public final class NfcAdapter { throw new IllegalArgumentException("context cannot be null"); } context = context.getApplicationContext(); - /* use getSystemService() instead of just instantiating to take - * advantage of the context's cached NfcManager & NfcAdapter */ + if (context == null) { + throw new IllegalArgumentException( + "context not associated with any application (using a mock context?)"); + } + /* use getSystemService() for consistency */ NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE); return manager.getDefaultAdapter(); } diff --git a/core/java/android/nfc/NfcManager.java b/core/java/android/nfc/NfcManager.java index 6ec2e219d5c4c..2bbed57482a1d 100644 --- a/core/java/android/nfc/NfcManager.java +++ b/core/java/android/nfc/NfcManager.java @@ -40,6 +40,10 @@ public final class NfcManager { public NfcManager(Context context) { NfcAdapter adapter; context = context.getApplicationContext(); + if (context == null) { + throw new IllegalArgumentException( + "context not associated with any application (using a mock context?)"); + } try { adapter = NfcAdapter.getNfcAdapter(context); } catch (UnsupportedOperationException e) {