我在一个 java 类中有一个方法,它被调用来设置该类的一个字段。该字段的类型为“char [] . As I try and access an element of the
char []` 我的程序将崩溃。
jni代码:
mid = env->GetMethodID(cls,"setData","([C)V");
env->CallVoidMethod(obj,mid,MyClass.Data) //MyClass.Data: unsigned char Data [8];
爪哇代码:
public void setData(char[] data2) { //Data: char [] Data = new char [8];
System.out.println("In Method"); //"In Method" is printed to console so
//Data = data2.clone(); //i know im calling the method correctly
for(int i = 0; i < 8; i++){
Data[i] = data2[i];}
}
我已经让它工作了,但只能通过改变方法的签名:
//jni side
mid = env->GetMethodID(cls,"setData","(CCCCCCCC)V");
env->CallVoidMethod(obj,mid,MyClass.Data[0],MyClass.Data[1],MyClass.Data[2],MyClass.Data[3],MyClass.Data[4],
MyClass.Data[5],MyClass.Data[6],MyClass.Data[7]);
//java side
public void setData(char c1,char c2,char c3,char c4,char c5,char c6,char c7,char c8) {
Data[0] = c1;
Data[1] = c2;
Data[2] = c3;
Data[3] = c4;
Data[4] = c5;
Data[5] = c6;
Data[6] = c7;
Data[7] = c8;
}
如何将方法与数组一起使用?稍后在程序中,我将更大的数组作为字段,并且使用起来不会那么混乱。