diff options
author | Nick Pelly <npelly@google.com> | 2011-11-21 17:02:02 -0800 |
---|---|---|
committer | Nick Pelly <npelly@google.com> | 2011-11-21 17:09:31 -0800 |
commit | b04cce0eb5917db436b7db39eb67c064df2e015a (patch) | |
tree | be8668d8919523a8f80dee56b1c240b4db84902a /core/java/android/nfc | |
parent | 5cf2a1399e8cd047e84e159e25c74c259882f52c (diff) | |
download | frameworks_base-b04cce0eb5917db436b7db39eb67c064df2e015a.zip frameworks_base-b04cce0eb5917db436b7db39eb67c064df2e015a.tar.gz frameworks_base-b04cce0eb5917db436b7db39eb67c064df2e015a.tar.bz2 |
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
Diffstat (limited to 'core/java/android/nfc')
-rw-r--r-- | core/java/android/nfc/NfcAdapter.java | 7 | ||||
-rw-r--r-- | 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 dec262b..b6af0f2 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 6ec2e21..2bbed57 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) { |