EasyNDK call method on main thread


#1

Hi,

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)
{
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

    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);

    #endif
}

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?


#2

Problem solved.

I migrate my code to use v3 of EasyNDK:

thy.


#3

Hi,

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.

1

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

MY JAVA 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 {
		
		@Override
		public void call(Session session, SessionState state, Exception exception) {
			onSessionStateChange(session, state, exception);
		}
		
}

2

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

3

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:


#4

Can anyone help me with it?


#5

Hi,

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) {
            return;
        }
        
        JNIEnv *pEnv = JniHelper::getEnv();
        if (!env) {
            return;
        }
        
        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.


#6

Hi alfonso.

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


#7

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.