15

I am getting this error JNI ERROR (app bug): accessed stale global reference When I run my app in Android OS v4, But when I run the same application in Android v2.3 I don't get this error.

This error occurs at the point where I call AsyncTask class, where I pass a string array as argument

Could Anyone Help me??

Alex Wiese
  • 8,142
  • 6
  • 42
  • 71
Surya2089
  • 269
  • 1
  • 2
  • 11
  • 2
    I remember reading in [Google release statement for developers](http://android-developers.blogspot.ie/2011/11/jni-local-reference-changes-in-ics.html) that ICS will have stricter verification of possible multi-threaded bugs, perhaps you just bumped into that? – skolima Aug 23 '12 at 07:37

4 Answers4

15

This error occurs when you call a method with an incorrect parameter type.

Make sure your method signature matches exactly what you are passing. For a string array:

jmethodID mid = env->GetMethodID(cls, methodName, "([Ljava/lang/String;)V");

If you are creating it yourself, it would look something like this:

jclass stringCls = env->FindClass("java/lang/String");
jobjectArray mStringArray = env->NewObjectArray( mSize, stringCls, NULL);

In your specific case, you are most likely not seeing the crash on Android 2.3 because you are calling AsyncTask.execute() which wasn't available until API 11 (Android 3.0) and your jmethodID is null. (It's a good idea to always check jclass and jmethodID for null after getting them)

Krys Jurgowski
  • 2,871
  • 17
  • 25
  • Just for future reference I misspelled `jlong` with `long`, which resulted in same error – ZdaR Sep 30 '16 at 20:49
  • Very helpful. In my case, I had omitted one of the method's declared arguments when I was performing a CallVoidMethod() call. This would be consistent with "occurs when you call a method with an incorrect parameter type". – Swampie Sep 03 '20 at 08:04
3

This error occurs when you call a method with an incorrect parameter type.

Addition, in this case you may be register the native method on Java code different from the native code. The difference can be you declare more or less parameters between the Java code and native code.

Thuan Bui
  • 41
  • 3
1

JNI Local Reference Changes in ICS

Crossle Song
  • 10,104
  • 2
  • 29
  • 38
  • @Crossle... mate i m new to android and facing the same problem http://stackoverflow.com/questions/21423036/sqlite3-exception-database-disk-image-is-malformed pls tell how can i implement your code.. i m rectifying it from last 3 weeks :( – Maveňツ Jan 31 '14 at 12:31
0

This applies to kotlin:

To add to what @krys has already mentioned, make sure that signatures on Kotlin side match exactly what you have in the JNI code. Even a simple Void (avoid explicit Void at the end of function signatures that return void on the JNI side) signature at the end of kotlin reference may fail and would make your debugging extremely frustrating.

Knight Forked
  • 1,529
  • 12
  • 14