How to get activity object via JNI


#1

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{
Log.i;
}
public static Object cppCall_logsth{
Log.i;
return activity;
}

JniMethodInfo minfo;
jobject jobj;
bool b = JniHelper::getStaticMethodInfoLjava/lang/Object;");
if {
LOGD (“JniHelper::getStaticMethodInfo error…”);
}else{
jobj = minfo.env~~>CallStaticObjectMethod;
}
JniHelper::getMethodInfoV");
if{
LOGD (“JniHelper::getMethodInfo error…”);
}else{
LOGD (“ready to invoke method…”);
minfo.env~~>CallVoidMethod;
}
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?


#2

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


#3

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!


#4

I think this link is useful http://java.sun.com/docs/books/jni/.


#5

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);
minfo.env~~>DeleteLocalRef(strOrder);
minfo.env->DeleteLocalRef(minfo.classID);
[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(ContextWrapper.java:347)
09-06 11:46:45.229: E/AndroidRuntime(31141): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:155)
09-06 11:46:45.229: E/AndroidRuntime(31141): at java.lang.StringBuilder.append(StringBuilder.java:217)
09-06 11:46:45.229: E/AndroidRuntime(31141): at com.halfmeters.sangosloto.AliPayActivity.pay(AliPayActivity.java:322)
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(Cocos2dxRenderer.java:79)
09-06 11:46:45.229: E/AndroidRuntime(31141): at org.cocos2dx.lib.Cocos2dxGLSurfaceView$10.run(Cocos2dxGLSurfaceView.java:345)
09-06 11:46:45.229: E/AndroidRuntime(31141): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1326)
09-06 11:46:45.229: E/AndroidRuntime(31141): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118)

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


#6

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)