@@ -61,6 +61,11 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
6161
6262 private fun handleMethodCall (call : MethodCall , result : Result ) {
6363
64+ if (activity == null ) {
65+ result.error(" 500" , " Cannot call method when not attached to activity" , null )
66+ return
67+ }
68+
6469 val nfcAdapter = getDefaultAdapter(activity)
6570
6671 if (nfcAdapter?.isEnabled != true && call.method != " getNFCAvailability" ) {
@@ -123,7 +128,9 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
123128 } catch (ex: IOException ) {
124129 Log .e(TAG , " Close tag error" , ex)
125130 }
126- nfcAdapter.disableReaderMode(activity)
131+ if (activity != null ) {
132+ nfcAdapter.disableReaderMode(activity)
133+ }
127134 result.success(" " )
128135 }
129136 }
@@ -301,7 +308,9 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
301308 private fun pollTag (nfcAdapter : NfcAdapter , result : Result , timeout : Int , technologies : Int ) {
302309
303310 pollingTimeoutTask = Timer ().schedule(timeout.toLong()) {
304- nfcAdapter.disableReaderMode(activity)
311+ if (activity != null ) {
312+ nfcAdapter.disableReaderMode(activity)
313+ }
305314 result.error(" 408" , " Polling tag timeout" , null )
306315 }
307316
0 commit comments