Ho un piccolo progetto con le librerie cocos2d-x. Sto cercando di utilizzare C++ per chiamare una funzione Java, ma ho un 11 un'eccezione segnale a linea:Impossibile effettuare chiamate da C++ a Java utilizzando JNI
// Get Status
status = jvm->GetEnv((void **) &env, JNI_VERSION_1_6);
Ma non so perché questo sta accadendo.
Nella mia classe Java Getsocial.java esiste questa funzione:
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)); }
Questa funzione lancio navigatore per inviare un tweet. Chiamato da Java funziona bene.
Nel mio C++ InterfaceJNI.h ho:
#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__
E 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; }
Questa interfaccia viene chiamato da una parte del codice utilizzando:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) InterfaceJNI::postMessageToTweet(); #elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) ObjCCalls::trySendATweet(); #endif
Cosa sta accadendo per restituire un puntatore nullo su jvm-> GetEnv ((void **) & env, JNI_VERS ION_1_6); ?
Questo funziona bene per lanciare la funzione, ora fallisce un altro con ma grazie! – vgonisanz