android - Calling a method in a Java object from JNI returns error -
i know there many questions asked before, cannot figure out why code not working. have simple class stores string:
public class jnistring { private string mstring; public jnistring() {} public string getstring() { return mstring; } public void setstring(string s) { mstring = s; } }
my jni code (in c), supposed take jnistring object argument, call setstring() function store data, , return integer:
jniexport jint jnicall java_com_sample_myclass_test(jnienv *env, jobject thiz, jobject jnistring) { jclass clazz = (*env)->findclass(env, "com/sample/jnistring"); jmethodid methodid = (*env)->getmethodid(env, clazz, "setstring", "(ljava/lang/string;)v"); jstring placeholder = (*env)->newstringutf(env, "placeholder"); (*env)->callvoidmethod(env, clazz, methodid, placeholder); return 1; }
and java code:
jnistring jnistring = new jnistring(); // call test() jni function jnistring argument
but reason, keeps failing following error message:
05-24 23:27:23.099 7575-7575/? a/art﹕ art/runtime/java_vm_ext.cc:410] jni detected error in application: can't call void com.sample.jnistring.setstring(java.lang.string) on instance of java.lang.class<com.sample.jnistring> 05-24 23:27:23.099 7575-7575/? a/art﹕ art/runtime/java_vm_ext.cc:410] in call callvoidmethod
i read having call newglobalref() or since ics (i'm running app on kitkat+) on here: http://android-developers.blogspot.ie/2011/11/jni-local-reference-changes-in-ics.html
but can't understand blog post saying, or put in code. appreciated. thanks!
edit: modified jni code following:
jniexport jint jnicall java_com_sample_myclass_test(jnienv *env, jobject thiz, jobject jnistring) { jclass clazz = (*env)->getobjectclass(env, jnistring); jmethodid methodid = (*env)->getmethodid(env, clazz, "setstring", "(ljava/lang/string;)v"); jstring placeholder = (*env)->newstringutf(env, "placeholder"); (*env)->callvoidmethod(env, jnistring, methodid, placeholder); return 1; }
but i'm getting different error:
05-25 19:11:02.832 8802-8802/com.sample e/art﹕ jni error (app bug): attempt use stale local reference 0x1 (should 0x100001) 05-25 19:11:02.850 8802-8802/com.sample a/art﹕ art/runtime/java_vm_ext.cc:410] jni detected error in application: use of deleted local reference 0x1
you're trying call instance method via class instead of via instance. second parameter needs object instance of jnistring
, in case jnistring
.
Comments
Post a Comment