我可以通过 JNI 调用非静态 java 方法,如下所示:
[C++代码]
jclass classID = minfo.env->FindClass("com/halfmeters/sangosloto/AliPayActivity");
jmethodID method = minfo.env->GetMethodID(classID,"pay","(Ljava/lang/String;)Z");
jobject obj = minfo.env->NewObject(classID, method);
jstring strOrder = minfo.env->NewStringUTF("test");
jboolean result = minfo.env->CallBooleanMethod(obj, method,strOrder);//minfo.methodID);
minfo.env->DeleteLocalRef(strOrder);
minfo.env->DeleteLocalRef(minfo.classID);
【java方法声明】
public boolean pay(String orderInfo){
System.out.println("Order info:" + orderInfo); // caught an exception
}
现在,当在 C++ 中执行该函数时,它会在 java 端引发异常,如下所示:
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.test.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)
任何人都可以帮助检查它有什么问题吗?谢谢!PS我发现java方法pay(String)在这个过程中被调用了两次,但我不知道它是怎么来的。