Question

J'ai un petit projet avec les bibliothèques Cocos2D-X.J'essaie d'utiliser C ++ pour appeler une fonction Java, mais je reçois un signal 11 exception à la ligne:

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

Mais je ne sais pas pourquoi cela se passe.

dans ma classe java getsocial.java existe cette fonction:

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));
    }

Cette fonction lancez le navigateur pour poster un tweet.Appelé de Java fonctionne bien.

dans mon interfacejni.h de mon C ++ j'ai:

#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__

et dans 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;
}

Cette interface est appelée à partir d'une partie du code à l'aide de:

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

Que se passe-t-il de retourner un pointeur nul sur jvm-> getenv ((vide **) & env, jni_version_1_6);?

Était-ce utile?

La solution

On dirait que votre variable JVM est nulle ou ordures.La version de Cocos2D-X que j'utilise a une classe appelée Jnihelper avec une statique :: getjavavm ();Méthode que vous pourriez vouloir utiliser.

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

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

Aussi, n'oubliez pas de "rafraîchir" votre projet Eclipse à chaque fois que vous construisez avec NDK.Vous faites probablement déjà déjà, mais ça vaut la peine de vérifier.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top