c++ - Access array from Java-side -
i have method in java class called set field of class. field of type "char []. try , access element of the
char []` program crash.
jni code:
mid = env->getmethodid(cls,"setdata","([c)v"); env->callvoidmethod(obj,mid,myclass.data) //myclass.data: unsigned char data [8];
java code:
public void setdata(char[] data2) { //data: char [] data = new char [8]; system.out.println("in method"); //"in method" printed console //data = data2.clone(); //i know im calling method correctly for(int = 0; < 8; i++){ data[i] = data2[i];} }
i have made work, changing signature of method:
//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; }
how can use method array? later in program have bigger arrays fields , less messy use one.
a java char array not same thing c char array:
- a char array in java object includes array length in addition data
- a char in java 16 bits, typically twice size of char in c
the jni api has functions creating java arrays , setting elements: newchararray, getchararrayelements, , releasechararrayelements.
Comments
Post a Comment