2013-03-25 8 views
5

Chcę nawiązać połączenie z C++ na Javę. Próbuję wywołać funkcję, która powoduje zmianę wartości bool, bez parametrów.Funkcja powrotu do kodu JNI funkcji 7, która próbuje wywołać Java z C++

To jest mój kodu C++

/** 
* Check if Internet Connection is ONLINE 
*/ 
bool InterfaceJNI::isInternetConnected() 
{ 
    JavaVM* jvm = JniHelper::getJavaVM(); 
    int status; 
    JNIEnv *env; 
    jmethodID mid; 

    bool isAttached = false; 
    // jboolean o bool? 
    bool returnValue = false; 

    CCLog("Static isInternetConnected"); 

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

    if(status < 0) 
    { 
     //LOGE("callback_handler: failed to get JNI environment, " // "assuming native thread"); 
     status = jvm->AttachCurrentThread(&env, NULL); 
     CCLog("isInternetConnected Status 2: %d", status); 
     if(status < 0) 
     { 
      // LOGE("callback_handler: failed to attach " // "current thread"); 
      return false; 
     } 
     isAttached = true; 
     CCLog("isInternetConnected Status isAttached: %d", isAttached); 
    } 


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

    jclass mClass = env->FindClass("org/example/SocialNetwork/InternetConnection"); 

    // Get Static bool isInternetConnection() 
    mid = env->GetStaticMethodID(mClass, "isInternetConnection", "()Z"); 
    if (mid == 0) 
    { 
     CCLog("isInternetConnected FAIL GET METHOD STATIC"); 
     return false; 
    } 
    // Call Static bool isInternetConnection() 
    returnValue = env->CallStaticBooleanMethod(mClass, mid); 
    CCLog("isInternetConnected Done "); 

      //----------------------------------------------------------- 
    CCLog("Finish"); 
    if(isAttached) 
     jvm->DetachCurrentThread(); 

    // Change for return value 
    return returnValue; 
} 

A mój kod Java:

public class InternetConnection 
{ 
    /** 
    * Check if is working your hello world from C++ 
    */ 
    public static void helloWorld() 
    { 
     Log.v("InternetConnection", "HELLO WORLD"); 
    } 
    /** 
    * Check Internet Connection 
    * @return true is Online 
    */ 
    public static Boolean isInternetConnection() 
    { 
     Log.v("InternetConnection", "isInternetConnection Start"); 

     Context ctx = CCSocialNetwork.getAppContext(); 
     ConnectivityManager conMgr = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo i = conMgr.getActiveNetworkInfo(); 

     if (i == null) 
     { 
      Log.v("InternetConnection", "isInternetConnection NULL :S"); 
      return false; 
     } 

     if (!i.isConnected()) 
     { 
      Log.v("InternetConnection", "isInternetConnection is not connected"); 
      return false; 
     } 

     if (!i.isAvailable()) 
     { 
      Log.v("InternetConnection", "isInternetConnection is not available"); 
      return false; 
     } 
     Log.v("InternetConnection", "isInternetConnection DONE!"); 
     return true; 
    } 
} 

Ale otrzymuję:

Fatal signal 7 (SIGBUS) at 0x00000000 (code=128) 

I mam Jeśli mogę uzyskać wartości zwracanej poprawnie , Nie byłbym w stanie wysłać parametrów.

+1

Zwracasz wartość logiczną, która w rzeczywistości jest obiektem. Spróbuj zamiast tego zwrócić typ boolowski typu prymitywnego. – maba

+0

Próbowałem dodać boolean vaar w Javie, ale nie jest to – vgonisanz

+0

Linia, która zawiedzie jest "mid = env-> GetStaticMethodID (mClass," isInternetConnection ","() Z ");" jeśli skomentuję to i następujące, jeśli sygnał nie pojawi się – vgonisanz

Odpowiedz

3

Zwracasz wartość logiczną, która w rzeczywistości jest obiektem. Spróbuj zamiast tego zwrócić typ boolowski typu prymitywnego.

0

Czy jesteś pewien, że FindClass nie zwrócił wartości NULL? Nie sprawdzasz tego