Issue with NDK Android module and ActivityThread

You must Login before you can answer or comment on any questions.

All, I wanted to put out a general question. I am running into an issue where my module is calling Android code (android.nfc.NfcAdaptor.enableForegroundDispatch) and I am getting a NullPointer inside the android code. I have identified the problem with the following line of code:

ActivityThread.currentActivityThread().registerOnActivityPausedListener(activity, mForegroundDispatchListener);
Has anyone run into this issue? As a result I may need to revert to the old way of building modules to get around this issue.

Tony

— asked 12 months ago by Tony Nuzzi
0 Comments

1 Answer

I found the possible answer. I am not sure if Android change or Appcelerator however I found something that worked for me. In short there is not active "Activity Threads" in "ActivityThread" (no pun intended). ActivityThread has a ThreadLocal storage that stores all of the active ActivityThreads. Classes like android.nfc.NfcAdaptor call ActivityThread.currentActivityThread().doSomething. However, if there are no active threads in ActivityThread the call to currentActivityThread() will return null thus resulting in a NullPointer in the android code.

To fix this problem in my coded I added the following to one of my @Kroll.method:

if(ActivityThread.currentActivityThread() == null)
        {
            ActivityThread.systemMain();
        }
NOTE: I tried to add in the onAppCreate however Java throw an exception so I moved it to the method that calls the ActivityThread (my method calls nfcAdaptor.enableForegroundDispatch) which in turn calls ActivityThread.currentActivityThread().

I am not sure if this is correct and would really like some from Appcelerator dev to chime in and correct me if I am wrong.

NOTE 2: The same code without the workaround above worked in 1.7.6, prior to V8 and the change to the android module development. Not sure if this was missed (e.g. regression) or something new needed.

Your Answer

Think you can help? Login to answer this question!