2
從C++觸發Java方法的時候才能通過JNI調用非靜態Java方法象下面這樣:的NullPointerException通過JNI
[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)
任何人都可以幫助檢查它有什麼問題嗎?謝謝! P.S.我發現在這個過程中java方法pay(String)被調用兩次,但我不知道它是如何發生的。如果此方法是從本機端稱爲
我覺得orderInfo沒有正確傳遞給Java –
我只是不知道如何找到問題。實際上,我在java方法中有一個空值驗證,在調用時不會觸發它。 – user1651035
當您嘗試複製orderInfo('orderInfo = new String(orderInfo)')時會發生什麼? –