EasyNDK call method on main thread



Im near to finish the EasyNDK implementation on my game.

The problem is when i try call a native method from JNI.

My sample code will fire a Java method and after this Java method will call another native (c++) method.

My C++ code is (this is OK and java is receiving it with success - no problem here - i think):

void MenuScene::menuFacebookCallback(Ref *sender)

    NDKHelper::AddSelector("MenuSceneSelectors", "showAlert", callfuncND_selector(MenuScene::onFacebookLogin), this);

    Dictionary* prms = Dictionary::create();
    prms->setObject(String::create("onFacebookLogin"), "call");
    prms->setObject(String::create("Muito bem, funcionou!"), "textToShow");

    SendMessageWithParams(string("showAlert"), prms);


My Java code that receive the “showAlert” call and need call a C++ confirmation method:

public class GameActivity extends Cocos2dxActivity {
public void showAlert(final JSONObject prms) {
        runOnUiThread( new Runnable() 
            public void run() 
            	try {
            		String functionToCall = prms.getString("call");
					if (!functionToCall.isEmpty()) {
						JSONObject params = new JSONObject();
						params.put("show", "StageSelect");
						AndroidNDKHelper.SendMessageWithParameters(functionToCall, params);
				} catch (JSONException e) {

Then i call “AndroidNDKHelper.SendMessageWithParameters(functionToCall, params);” i got errors in image:

After search this error happen when we are running code in difference thread or trying to call a method from other thread. But im in the Game Activity, this is the starter activity and im using “runOnUiThread”.

What i do?


Problem solved.

I migrate my code to use v3 of EasyNDK:




The problem persist, but now is in other scenario. After facebook login success or logout success i call a native method to change the scene.


When i call from facebook event i got the thread error, but the method is called on native code:


public void onSessionStateChange(Session session, SessionState state, Exception exception) {
		if (state.isOpened()) {
			Log.i(TAG, "Logged in...");			
			JSONObject params = new JSONObject();
			AndroidNDKHelper.SendMessageWithParameters("onFacebookLoginOK", params);
		} else if (state.isClosed()) {
			Log.i(TAG, "Logged out...");
			JSONObject params = new JSONObject();
			AndroidNDKHelper.SendMessageWithParameters("onFacebookLoginError", params);

private class SessionStatusCallback implements Session.StatusCallback {
		public void call(Session session, SessionState state, Exception exception) {
			onSessionStateChange(session, state, exception);


The other two errors that i dont know what and why happen, is in this image:


I dont know why but im receiving a facebook error. My hash key is OK, i generate using command tool and using java code, both are the same and configure on facebook page. The login process happen normally, but on log i see this errors:


Can anyone help me with it?



As far as that first problem, I think I’ve seen something like that before and might have a workaround. Inside your NDKHelper.cpp, try the following for your “void Java_com_easyndk_classes_AndroidNDKHelper_CPPNativeCallHandler()” method:

        /* The JniHelper call resulted in crash, so copy the jstring2string method here */
        //std::string jsonString = JniHelper::jstring2string(json);
        if (json == NULL) {
        JNIEnv *pEnv = JniHelper::getEnv();
        if (!env) {
        const char *chars = env->GetStringUTFChars(json, NULL);
        std::string ret(chars);
        env->ReleaseStringUTFChars(json, chars);
        std::string jsonString = ret;
        /* End jstring2string code */
        const char *jsonCharArray = jsonString.c_str();
        json_error_t error;
        json_t *root;
        root = json_loads(jsonCharArray, 0, &error);

I commented out the “JniHelper::jstring2string()” call and copied the code that was in that method straight into NDKHelper. Please let me know if that helps.


Hi alfonso.

The problem was not solved. With your code it still working, but with the same problem.


As i post images and the issue here:https://github.com/alfonsocejudo/EasyNDK-for-cocos2dx3/issues/2

I think that the cocos2dx is destroying the scene and ndkhelper try to remove or release the released scene. If you see on images on github, you will see that cocos engine try access a Null object.