I am trying to get my app to launch via NFC AAR, but ignore all NFC intents thereafter (I don't want my app to get launched when its already running). After sifting around here, I found the best way to do that is by enabling foregroundDispatch during my activities. I tried to follow others syntax as closely as possible, but now my app is crashing ("Unfortunately, MyApp has stopped"). Any help would be very much appreciated
Manifest.xml:
<application
<activity
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="application/com.MyApp.frontcam" />
</intent-filter>
</activity>
</application>
Activity.Java:
private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;
private IntentFilter[] mFilters;
private String[][] mTechLists;
@Override
public void onCreate(Bundle savedInstanceState) {
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("*/*");
}
catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
mFilters = new IntentFilter[] {ndef, };
}
@Override
protected void onResume() {
mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, null);
}
Logcat:
09-11 22:28:31.205: E/CameraPreview(2047): 176/144
09-11 22:29:04.756: D/AndroidRuntime(2047): Shutting down VM
09-11 22:29:04.756: W/dalvikvm(2047): threadid=1: thread exiting with uncaught exception (group=0xb0d0cb20)
09-11 22:29:04.826: E/AndroidRuntime(2047): FATAL EXCEPTION: main
09-11 22:29:04.826: E/AndroidRuntime(2047): Process: com.MyApp.frontcam, PID: 2047
09-11 22:29:04.826: E/AndroidRuntime(2047): java.lang.RuntimeException: Unable to resume activity {com.MyApp.frontcam/com.MyApp.frontcam.MainActivity}: java.lang.NullPointerException
09-11 22:29:04.826: E/AndroidRuntime(2047): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2788)
09-11 22:29:04.826: E/AndroidRuntime(2047): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2817)
09-11 22:29:04.826: E/AndroidRuntime(2047): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
09-11 22:29:04.826: E/AndroidRuntime(2047): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-11 22:29:04.826: E/AndroidRuntime(2047): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-11 22:29:04.826: E/AndroidRuntime(2047): at android.os.Handler.dispatchMessage(Handler.java:102)
09-11 22:29:04.826: E/AndroidRuntime(2047): at android.os.Looper.loop(Looper.java:136)
09-11 22:29:04.826: E/AndroidRuntime(2047): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-11 22:29:04.826: E/AndroidRuntime(2047): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 22:29:04.826: E/AndroidRuntime(2047): at java.lang.reflect.Method.invoke(Method.java:515)
09-11 22:29:04.826: E/AndroidRuntime(2047): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-11 22:29:04.826: E/AndroidRuntime(2047): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-11 22:29:04.826: E/AndroidRuntime(2047): at dalvik.system.NativeStart.main(Native Method)
09-11 22:29:04.826: E/AndroidRuntime(2047): Caused by: java.lang.NullPointerException
09-11 22:29:04.826: E/AndroidRuntime(2047): at com.MyApp.frontcam.MainActivity.onResume(MainActivity.java:106)
09-11 22:29:04.826: E/AndroidRuntime(2047): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
09-11 22:29:04.826: E/AndroidRuntime(2047): at android.app.Activity.performResume(Activity.java:5310)
09-11 22:29:04.826: E/AndroidRuntime(2047): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2778)
09-11 22:29:04.826: E/AndroidRuntime(2047): ... 12 more
It looks like mAdapter
is null in your onResume
function.
Make sure you initialize it before you use the variable.
Looking at the documentation, you first need to get the NfcManager
, and get an NfcAdapter
instance from there.
You can create a getter method for the adapter, so that you only set the value if it hasn't been done already. Then in onResume
, you can call the method to get your adapter.
private NfcAdapter getAdapter() {
if (mAdapter == null) {
NfcManager manager = (NfcManager) getSystemServive(NFC_SERVICE);
mAdapter = manager.getDefaultAdapter(this);
}
return mAdapter;
}
@Override
protected void onResume() {
super.onResume();
getAdapter().enableForegroundDispatch(this, mPendingIntent, mFilters, null);
}