2012-10-01 20 views
6

Tengo un pequeño proyecto con las bibliotecas cocos2d-x. Estoy tratando de utilizar C++ para llamar a una función de Java pero me da una señal en la línea 11 de excepción:No puedo hacer una llamada desde C++ a Java usando JNI

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

Pero yo no sé por qué ocurre esto.

En mi clase Java Getsocial.java existe esta función:

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

Esta función de lanzamiento del navegador para publicar un tweet. Llamado desde Java funciona bien.

En mi C++ InterfaceJNI.h tengo:

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

Y en 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; 
} 

Esta interfaz se llama desde una parte del código usando:

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

¿Qué está pasando para devolver un puntero nulo en jvm-> GetEnv ((void **) & env, JNI_VERS ION_1_6); ?

Respuesta

2

Parece que su variable jvm es nula o basura. La versión de Cocos2D-x que uso tiene una clase llamada JniHelper con static :: getJavaVM(); método que puede usar.

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

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

Además, recuerde "actualizar" su proyecto eclipse cada vez que compila con NDK. Probablemente ya lo hagas, pero vale la pena verificarlo.

+0

Esto funciona bien para iniciar la función, ahora falla otro pero ¡gracias! – vgonisanz

Cuestiones relacionadas