0

我正在尝试创建一个对象的实例,该实例具有一个构造函数,该构造函数通过将整数作为 jvalue 数组的成员传递来获取两个整数。当我从构造函数打印参数时,似乎只有第一个参数被正确传递,为什么会这样?我的 C 和 Java 代码如下。

C代码

jclass theClass;
jmethodID theMethod;
theClass = (*env)->FindClass(env, "thepackage/TwoNumbers");
theMethod = (*env)->GetMethodID(env, theClass, "<init>", "(II)V");

jvalue args[2];
args[0].i=55;
args[1].i=6;

jobject theObj = (*env)->NewObject(env, theClass, theMethod, *args);

Java 代码

package thepackage;
public class TwoNumbers {
    int a;
    int b;

    TwoNumbers(int first, int second) {
        this.a=first;
        this.b=second;
        System.out.println("A is "+first+" and b is "+second);
    }
}
4

1 回答 1

2

您正在调用NewObject采用可变长度参数列表的函数。要使用 jvalue 参数版本,您必须调用NewObjectA.

jobject theObj=(*env)->NewObjectA(env,theClass,theMethod,*args);

请参阅文档 - NewObject

于 2013-10-10T17:32:03.503 回答