Merge "Throw a nicer error message when using an invalid context to create Nfc"

This commit is contained in:
Nick Pelly
2011-11-21 17:31:45 -08:00
committed by Android (Google) Code Review
2 changed files with 9 additions and 2 deletions

View File

@@ -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);
if (manager == null) {
// NFC not available

View File

@@ -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) {