@@ -61,6 +61,11 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
61
61
62
62
private fun handleMethodCall (call : MethodCall , result : Result ) {
63
63
64
+ if (activity == null ) {
65
+ result.error(" 500" , " Cannot call method when not attached to activity" , null )
66
+ return
67
+ }
68
+
64
69
val nfcAdapter = getDefaultAdapter(activity)
65
70
66
71
if (nfcAdapter?.isEnabled != true && call.method != " getNFCAvailability" ) {
@@ -123,7 +128,9 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
123
128
} catch (ex: IOException ) {
124
129
Log .e(TAG , " Close tag error" , ex)
125
130
}
126
- nfcAdapter.disableReaderMode(activity)
131
+ if (activity != null ) {
132
+ nfcAdapter.disableReaderMode(activity)
133
+ }
127
134
result.success(" " )
128
135
}
129
136
}
@@ -301,7 +308,9 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
301
308
private fun pollTag (nfcAdapter : NfcAdapter , result : Result , timeout : Int , technologies : Int ) {
302
309
303
310
pollingTimeoutTask = Timer ().schedule(timeout.toLong()) {
304
- nfcAdapter.disableReaderMode(activity)
311
+ if (activity != null ) {
312
+ nfcAdapter.disableReaderMode(activity)
313
+ }
305
314
result.error(" 408" , " Polling tag timeout" , null )
306
315
}
307
316
0 commit comments