How to get activity object via JNI


recently I’m studying call java methods from C*+ with JNI, and there’s a non-static method in java which should be triggered by C**, then I found below sample codes:

public void cppCall_nonStatic_logsth{
public static Object cppCall_logsth{
return activity;

JniMethodInfo minfo;
jobject jobj;
bool b = JniHelper::getStaticMethodInfoLjava/lang/Object;");
if {
LOGD (“JniHelper::getStaticMethodInfo error…”);
jobj = minfo.env~~>CallStaticObjectMethod;
LOGD (“JniHelper::getMethodInfo error…”);
LOGD (“ready to invoke method…”);
the main process is to call a static java method to get activity object, and then using this object to call a non-static java method.
however, i cannot find such a static activity object in Cocos2dxActivity. Is anyone knows anything about this issue, or other sample codes to trigger a non-static java method from C*+ via JNI?


You pass java instance to native, then keep reference.
Then you can call CallObjectMethod() to invoke a instance method.


Minggo Zhang wrote:

You pass java instance to native, then keep reference.
Then you can call CallObjectMethod() to invoke a instance method.

sorry i’m a beginer in this part. is there any detail about how to “pass java instance to native, then keep reference”? thx!


I think this link is useful


Now I could call a non-static java method via JNI like below:

[C*+ code]
jclass classID = minfo.env~~>FindClass;
jmethodID method = minfo.env~~>GetMethodIDZ“);
jobject obj = minfo.env~~>NewObject;
jstring strOrder = minfo.env~~>NewStringUTF(”test“);
jboolean result = minfo.env~~>CallBooleanMethod;//minfo.methodID);
[java method declaration]
public boolean pay(String orderInfo){
System.out.println(”Order info:"* orderInfo); // caught an exception

now when executed the function in C++, it throws an exception by java side like below:

09-06 11:46:45.229: E/AndroidRuntime(31141): FATAL EXCEPTION: GLThread 12
09-06 11:46:45.229: E/AndroidRuntime(31141): java.lang.NullPointerException
09-06 11:46:45.229: E/AndroidRuntime(31141): at android.content.ContextWrapper.bindService(
09-06 11:46:45.229: E/AndroidRuntime(31141): at java.lang.AbstractStringBuilder.append0(
09-06 11:46:45.229: E/AndroidRuntime(31141): at java.lang.StringBuilder.append(
09-06 11:46:45.229: E/AndroidRuntime(31141): at
09-06 11:46:45.229: E/AndroidRuntime(31141): at org.cocos2dx.lib.Cocos2dxRenderer.nativeTouchesEnd(Native Method)
09-06 11:46:45.229: E/AndroidRuntime(31141): at org.cocos2dx.lib.Cocos2dxRenderer.handleActionUp(
09-06 11:46:45.229: E/AndroidRuntime(31141): at org.cocos2dx.lib.Cocos2dxGLSurfaceView$
09-06 11:46:45.229: E/AndroidRuntime(31141): at android.opengl.GLSurfaceView$GLThread.guardedRun(
09-06 11:46:45.229: E/AndroidRuntime(31141): at android.opengl.GLSurfaceView$

could anyone help to check what’s wrong with it? thanks!


in java method, i also added a validation like below:

if(“test”.equals(orderInfo)) return false;

it does not return here while i tried to send a “test” jstring object.(see above codes)