Android - NDK - c++ -不能初始化一个类型为'int *'的变量,该变量的rvalue为'jlong *'(又名'long long *')。

时间:2021-10-06 16:43:54

I asked a similar question about char to jsstring ... but now I have a problem with an int to jlongArray, which I just can't figure out :/

我问了一个类似的问题关于char和jsstring…但是现在我有一个问题,一个int到jlongArray,我只是不知道:/。

I get the following error:

我得到了以下错误:

Error:(290, 10) error: cannot initialize a variable of type 'int *' with an rvalue of type 'jlong *' (aka 'long long *')

on this line:

在这条线:

JNIEXPORT void Java_de_meetspot_ndktest_MainActivity_LoadPlayerA(JNIEnv *javaEnvironment, jobject self, jstring audioPath, jlongArray offsetAndLength) {
   const char* audio = javaEnvironment->GetStringUTFChars(audioPath, JNI_FALSE);
   int* params = javaEnvironment->GetLongArrayElements(offsetAndLength, JNI_FALSE);
   example->LoadPlayerA(audio, params);
}

this is the declaration:

这是声明:

void LoadPlayerA(const char *audioPath, int *params);

can someone help me out?

有人能帮我吗?

2 个解决方案

#1


2  

long in Java is a signed 64-bit integer type. A C++ int on the other hand is only defined as being "guaranteed to have a width of at least 16 bits".

在Java中,long是一个有符号的64位整数类型。另一方面,C++ int被定义为“保证至少有16位的宽度”。

Lets's assume that your ints are 32-bit (that's a very common scenario): so now you've got a pointer to a bunch of data where each element is 64 bits, and you're trying to pass that to a function that expects a pointer to 32-bit data. That's obviously not going to work, even if the compiler had allowed it.

假设您的ints是32位(这是一个非常常见的场景):所以现在您有了一个指向一堆数据的指针,其中每个元素都是64位,您正试图将其传递给一个函数,该函数期望指向32位数据的指针。这显然不会起作用,即使编译器允许。

Some options for you:

给你一些选项:

  • Change LoadPlayerA to take a jlong* or int64_t* (and change any code in LoadPlayerA that relies on the incoming data being of type int).
  • 更改LoadPlayerA以获取jlong*或int64_t*(并更改LoadPlayerA中的任何代码,该代码依赖于int类型的传入数据)。
  • Change the Java code to pass the data as an int[] instead of a long[], and change LoadPlayerA to take a jint* or int32_t*.
  • 更改Java代码以将数据传递为int[]而不是长[],并更改LoadPlayerA以获取jint*或int32_t*。
  • Allocate an array/vector of type int in your JNI function and convert the data from jlong to int before passing it to LoadPlayerA.
  • 在JNI函数中分配int类型的数组/向量,并将数据从jlong转换为int,然后将其传递给LoadPlayerA。

#2


0  

Thanks to Michael :) ..

感谢迈克尔:……

here is how i did it:

我是这样做的:

 jlong *longParams = javaEnvironment->GetLongArrayElements(params, JNI_FALSE);
   int arr[6];
   for (int n = 0; n < 6; n++) arr[n] = longParams[n];
   javaEnvironment->ReleaseLongArrayElements(params, longParams, JNI_ABORT);

#1


2  

long in Java is a signed 64-bit integer type. A C++ int on the other hand is only defined as being "guaranteed to have a width of at least 16 bits".

在Java中,long是一个有符号的64位整数类型。另一方面,C++ int被定义为“保证至少有16位的宽度”。

Lets's assume that your ints are 32-bit (that's a very common scenario): so now you've got a pointer to a bunch of data where each element is 64 bits, and you're trying to pass that to a function that expects a pointer to 32-bit data. That's obviously not going to work, even if the compiler had allowed it.

假设您的ints是32位(这是一个非常常见的场景):所以现在您有了一个指向一堆数据的指针,其中每个元素都是64位,您正试图将其传递给一个函数,该函数期望指向32位数据的指针。这显然不会起作用,即使编译器允许。

Some options for you:

给你一些选项:

  • Change LoadPlayerA to take a jlong* or int64_t* (and change any code in LoadPlayerA that relies on the incoming data being of type int).
  • 更改LoadPlayerA以获取jlong*或int64_t*(并更改LoadPlayerA中的任何代码,该代码依赖于int类型的传入数据)。
  • Change the Java code to pass the data as an int[] instead of a long[], and change LoadPlayerA to take a jint* or int32_t*.
  • 更改Java代码以将数据传递为int[]而不是长[],并更改LoadPlayerA以获取jint*或int32_t*。
  • Allocate an array/vector of type int in your JNI function and convert the data from jlong to int before passing it to LoadPlayerA.
  • 在JNI函数中分配int类型的数组/向量,并将数据从jlong转换为int,然后将其传递给LoadPlayerA。

#2


0  

Thanks to Michael :) ..

感谢迈克尔:……

here is how i did it:

我是这样做的:

 jlong *longParams = javaEnvironment->GetLongArrayElements(params, JNI_FALSE);
   int arr[6];
   for (int n = 0; n < 6; n++) arr[n] = longParams[n];
   javaEnvironment->ReleaseLongArrayElements(params, longParams, JNI_ABORT);