使用JNI CallBooleanMethod的异常信号11

时间:2013-03-26 20:44:38

标签: java java-native-interface

我在这一行得到了一个SIGNAL 11:

// Call bool isInternetConnection()
jboolean jReturnValue = env->CallBooleanMethod(mClass,mid);

在这段代码中:

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

    bool isAttached = false;
    bool returnValue = false;

    CCLog("InterfaceJNI isInternetConnected");

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

    if(status < 0)
    {
        CCLog("isInternetConnected Status < 0 Failed to get JNI Enviroment!!!");
        status = jvm->AttachCurrentThread(&env, NULL);
        CCLog("isInternetConnected Status 2: %d", status);
        if(status < 0)
        {
            CCLog("isInternetConnected Status < 0 !!!");
            return false;
        }
        isAttached = true;
        CCLog("isInternetConnected Status isAttached: %d", isAttached);
    }

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

    CCLog("isInternetConnected Finding Class....");
    jclass mClass = env->FindClass("org/example/SocialNetwork/CCSocialNetwork");

    // Get Static bool isInternetConnection()
    CCLog("isInternetConnected Getting method....");
    mid = env->GetMethodID(mClass, "isInternetConnection", "()Z");
    if (mid == 0)
    {
        CCLog("isInternetConnected FAIL GET METHOD STATIC");
        return false;
    }
    CCLog("isInternetConnected Calling method....");
    // Call bool isInternetConnection()
    jboolean jReturnValue = env->CallBooleanMethod(mClass,mid);
    CCLog("Call done ");
    // Convert value from Java to C++
    returnValue = (bool)jReturnValue;
    CCLog("isInternetConnected Done ");

    if(isAttached)
        jvm->DetachCurrentThread();

    // Change for return value
    return returnValue;
}

但我不知道为什么。如果我使用“()V”进行无效调用,但是当我尝试调用返回bool no的函数时,

发生了什么事?

1 个答案:

答案 0 :(得分:3)

它是否是静态方法?您在代码中的注释表明它是静态的,而您的方法调用则不是。

如果 NOT 是静态的,那么你必须用一个对象调用CallBooleanMethod,而不是像现在这样调用类。否则,您必须在课程中使用CallStaticBooleanMethod

jobject someObject = <get or create the object somehow>
jboolean jReturnValue = env->CallBooleanMethod(someObject,mid);

OR

jboolean jReturnValue = env->CallStaticBooleanMethod(mClass,mid);

无论哪种方式,您都必须决定是否使用静态方法。