java - JNI 返回 Signal 7 函数试图从 C++ 调用 Java

标签 java c++ java-native-interface signals cocos2d-x

我想从 C++ 调用 Java。我尝试调用一个不带参数的返回 bool 值的函数。

这是我的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;
}

还有我的 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;
    }
}

但是我得到:

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

如果我能正确获得返回值,我也无法发送参数。

最佳答案

您返回的是一个 boolean 值,它实际上是一个对象。尝试返回 boolean 原始类型。

关于java - JNI 返回 Signal 7 函数试图从 C++ 调用 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15623478/

相关文章:

java - 如何将java程序(log4j2)中的日志索引到elasticsearch中

java - 在 LeetCode 质数挑战赛中找不到我的程序的修复程序

c++ - SIGSEGV,段错误。 while printf() 数组索引的值

android - 崩溃初始化 std::String

Java Native Interface 偷偷摸摸的 fork 行为

java - java中执行cmd命令获取时间

java - 在 JPanel 上画一条可移动的线

C++整理负输出

c++ - Gtk:将焦点发送到顶层窗口而不丢失第一个顶层窗口

c++ - mno-cygwin 在构建 C++ 库时出现无法识别的命令行选项错误