我在java类中有一个方法,用于设置该类的字段。该字段的类型为“char [] . As I try and access an element of the
char []`我的程序将崩溃。
jni code:
mid = env->GetMethodID(cls,"setData","([C)V");
env->CallVoidMethod(obj,mid,MyClass.Data) //MyClass.Data: unsigned char Data [8];
java代码:
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;
}
如何将该方法与数组一起使用?在程序的后期,我将更大的数组作为字段,并且使用它时不那么混乱。
答案 0 :(得分:2)
java char数组与C char数组不同:
jni API具有创建Java数组和设置其元素的功能:NewCharArray,GetCharArrayElements和ReleaseCharArrayElements。