java - Android, NDK, JNI, "cannot initialize a variable of type ' 长 *' with an rvalue of type ' jlong​​ *' "

标签 java android c++ java-native-interface

我想得到一个 Java long[] 数组,并最终将它传递给需要 const long 输入类型的函数 powerEstimate . 这是 C++ 中的函数定义:

void powerEstimate(const double rr_data[], const long timeRR_data[])

这是我用来获取 Java long[] 数组的代码:

Java_com_example_matteofabris_stressmanagement_acquisition_SewAcquisition_powerEstimateNative(
    JNIEnv *env, jobject, jdoubleArray rr_j_, jlongArray timeRR_j_) {         
    // GET
    double* rr_j = env->GetDoubleArrayElements(rr_j_, NULL);
    long* timeRR_j = env->GetLongArrayElements(timeRR_j_, NULL);

    // TODO
    powerEstimate(rr_j, timeRR_j)

当我构建它时,我得到这个错误:

/Users/matteofabris/AndroidStudioProjects/StressManagement/app/src/main/cpp/native-lib.cpp:35:11: error: cannot initialize a variable of type 'long *' with an rvalue of type 'jlong *' (aka 'long long *')
long* timeRR_j = env->GetLongArrayElements(timeRR_j_, NULL);

有人可以解释为什么我会收到此错误以及如何解决吗? 提前感谢您的耐心等待。

最佳答案

long 在 Windows 上只有 32 位(看起来是您的平台)。但是,Java jlong​​ 是 64 位的。

您可以在 native 代码中使用 __int64* 代替 long,因为这也是 jlong​​jni_md 中定义的方式:

__int64* timeRR_j = env->GetLongArrayElements(timeRR_j_, NULL);

void powerEstimate(const double rr_data[], const __int64
               timeRR_data[])

如果您不能更改 powerEstimate 函数,那么您可以在 Java 端切换到 int[],如 int s 是 32 位,就像 native long 一样。

关于java - Android, NDK, JNI, "cannot initialize a variable of type ' 长 *' with an rvalue of type ' jlong​​ *' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53411003/

相关文章:

java - file.lastModified() 永远不是用 file.setLastModified() 设置的

java - 访问另一个类的微调器时出现 NullPointerException

c++ - 已弃用 API 调用的编译时检测?

c++ - Windows 8 : Application is not able write to C:\ProgramData\

javah 帮助 - "No classes were specified on the command line"

java - java.time API 如何确定政府对区域规则的更改?

java - Web服务仅限于单线程?

android - 更新 RecyclerView.Adapter

android - 如何让 Rails 告诉移动应用程序用户是否登录?

c++ - 不能包含 cliext header (对于 c++ cli、Visual Studio)