JNI is killing me! I am trying to call a non-static java method from c++. I am able to call static method without any problems but when I try to call a similar non static method I get a runtime error “Fatal signal 11 (SIGSEGV) at 0x00000004 (code=1).
So pretty much what I did is I looked that the CocosDenshion system so inside SimpleAudioEngineJNI.cpp and I setup a similar system in my class file. Here is what it looks like
<pre>
#include”platform/android/jni/JniHelper.h"
#include <android/log.h>
#define LOG_TAG “libSimpleAudioEngine”
#define LOGD (…) _android_log_print
#define CLASS_NAME “org/rocknrolla/games/Wrapper”
typedef struct JniMethodInfo
{
JNIEnv * env;
jclass classID;
jmethodID methodID;
} JniMethodInfo;
extern “C”
{
// get env and cache it
static JNIEnv* getJNIEnv(void)
{
JavaVM* jvm = cocos2d::JniHelper::getJavaVM();
if (NULL == jvm) {
LOGD ("Failed to get JNIEnv. JniHelper::getJavaVM() is NULL“);
return NULL;
}
JNIEnv env = NULL;
// get jni environment
jint ret = jvm~~>GetEnv&env, JNI_VERSION_1_4);
switch {
case JNI_OK :
// Success!
return env;
case JNI_EDETACHED :
// Thread not attached
// TODO : If calling AttachCurrentThread on a native thread
// must call DetachCurrentThread in future.
// see: http://developer.android.com/guide/practices/design/jni.html
if < 0)
{
LOGD (“Failed to get the environment using AttachCurrentThread()”);
return NULL;
} else {
// Success : Attached and obtained JNIEnv!
return env;
}
case JNI_EVERSION :
// Cannot recover from this error
LOGD (“JNI interface version 1.4 not supported”);
default :
LOGD (“Failed to get the environment using GetEnv()”);
return NULL;
}
}
// get class and make it a global reference, release it at endJni.
static jclass getClassID
{
jclass ret = pEnv~~>FindClass;
if
{
LOGD (“Failed to find class of %s”, CLASS_NAME);
}
return ret;
}
static bool getStaticMethodInfo
{
jmethodID methodID = 0;
JNIEnvpEnv = 0;
bool bRet = false;
do
{
pEnv = getJNIEnv();
if (! pEnv)
{
break;
}
jclass classID = getClassID(pEnv);
methodID = pEnv~~>GetStaticMethodID;
if
{
LOGD (“Failed to find static method id of %s”, methodName);
break;
}
methodinfo.classID = classID;
methodinfo.env = pEnv;
methodinfo.methodID = methodID;
bRet = true;
} while ;
return bRet;
}
static bool getMethodInfo
{
jmethodID methodID = 0;
JNIEnv *pEnv = 0;
bool bRet = false;
do
{
pEnv = getJNIEnv;
if
{
break;
}
jclass classID = getClassID;
methodID = pEnv~~>GetMethodID(classID, methodName, paramCode);
if (! methodID)
{
LOGD (“Failed to find method id of %s”, methodName);
break;
}
methodinfo.classID = classID;
methodinfo.env = pEnv;
methodinfo.methodID = methodID;
bRet = true;
} while (0);s
return bRet;
}
void upgradeAdFreeJNI()
{
LOGD (“upgradeAdFreeJNI”);
JniMethodInfo methodInfo;
if (! getStaticMethodInfo(methodInfo,”upgradeAdFree“,”()V“))
{
return;
}
methodInfo.env~~>CallVoidMethod;
methodInfo.env~~>DeleteLocalRef(methodInfo.classID);
}
}
</pre>
My java file is also brutally simple and I am just trying to get at the apps main activity:
<pre>
package org.rocknrolla.games;
import org.cocos2dx.lib.Cocos2dxActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import org.rocknrolla.games.util.*;
public class Wrapper extends Cocos2dxActivity{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
// User clicked the”Upgrade to Premium" button.
public void upgradeAdFree() {
Log.d(TAG, “Upgrade button clicked; launching purchase flow for upgrade.”);
}
}
Anyone else been able to call a non static method?
Thanks