Question

I have a little project with cocos2d-x libraries. I'm trying to use C++ to call a Java function but i get a signal 11 exception at line:

// Get Status
status = jvm->GetEnv((void **) &env, JNI_VERSION_1_6);

But i don't know why this is happening.

In my Java class Getsocial.java exist this function:

private void tweet()
    {
        String score = "123";
        String tweetUrl = "https://twitter.com/intent/tweet?text=Hello ! I have just got " + score + " points in mygame for Android !!!!";
        Uri uri = Uri.parse(tweetUrl);
        startActivity(new Intent(Intent.ACTION_VIEW, uri));
    }

This function launch navigator to post a tweet. Called from Java works fine.

In my C++ InterfaceJNI.h I have:

#ifndef __INTERFACE_JNI_H__
#define __INTERFACE_JNI_H__

#include "cocos2d.h"

class InterfaceJNI
{
public:
    static void postMessageToFB();
    static void postMessageToTweet();

protected:

};

#endif // __INTERFACE_JNI_H__

And in InterfaceJNI.cpp:

#include "InterfaceJNI.h"
#include "platform/android/jni/JniHelper.h"
#include  jni.h >
#include  android/log.h >

using namespace cocos2d;

void InterfaceJNI::postMessageToTweet()
{
    int status;
    JNIEnv *env;
    JavaVM *jvm;
    jmethodID mid;
    jclass mClass;
    bool isAttached = false;

    CCLog("Static postMessageToTweet");

    // Get Status
    status = jvm->GetEnv((void **) &env, JNI_VERSION_1_6);

    CCLog("Status: %d", status);

    if(status AttachCurrentThread(&env, NULL);
        CCLog("Status 2: %d", status);
        if(status GetStaticMethodID(mClass, "tweet", "()V");
    CCLog("mID: %d", mid);

    if (mid!=0)
        env->CallStaticVoidMethod(mClass, mid);
            //-----------------------------------------------------------
    CCLog("Finish");
    if(isAttached)
        jvm->DetachCurrentThread();

    return;
}

This interface is called from a part of the code using:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    InterfaceJNI::postMessageToTweet();
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    ObjCCalls::trySendATweet();
#endif

What is happening to return a null pointer on jvm->GetEnv((void **) &env, JNI_VERSION_1_6); ?

Was it helpful?

Solution

It looks like your jvm variable is null or garbage. The version of Cocos2D-x I use has a class called JniHelper with a static ::getJavaVM(); method that you might want to use.

JavaVM* vm = JniHelper::getJavaVM();
JNIEnv* env;

vm->GetEnv((void**)&env,JNI_VERSION_1_4);  // mine uses JNI_VERSION_1_4

Also, remember to "refresh" your eclipse project every time you build with NDK. You probably do already, but it's worth checking.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top