简体   繁体   中英

JNI ERROR (app bug): attempt to use stale Local 0x31 (should be 0x39)

Good day. Wrote code that should set key values.

extern "C" JNIEXPORT jboolean JNICALL Java_com_example_woodman_testlibsettingsv2_frmMain_SetSystemKeysString(JNIEnv *env, jobject instance, jstring key_Name_) { 
    const char *key_Name = env->GetStringUTFChars(key_Name_, 0);

    // TODO

    jclass secClass = env->FindClass("android/provider/Settings$System");
    jmethodID secMid = env->GetStaticMethodID(secClass, "putString", "(Landroid/content/ContentResolver;Landroid/net/Uri;Ljava/lang/String;Ljava/lang/String;)Z");

    jclass activityThread = env->FindClass("android/app/ActivityThread");
    jmethodID currentActivityThread = env->GetStaticMethodID(activityThread, "currentActivityThread", "()Landroid/app/ActivityThread;");
    jobject at = env->CallStaticObjectMethod(activityThread, currentActivityThread);
    jmethodID getApplication = env->GetMethodID(activityThread, "getApplication", "()Landroid/app/Application;");
    jobject context = env->CallObjectMethod(at, getApplication);

    jclass c_context = env->FindClass("android/content/Context");
    jmethodID m_get_content_resolver = env->GetMethodID(c_context, "getContentResolver", "()Landroid/content/ContentResolver;");
    jobject o_content_resolver;
    o_content_resolver = env->CallObjectMethod(context, m_get_content_resolver);

    jstring jStringParam = env->NewStringUTF(key_Name);
    jboolean jStringKey = env->CallStaticBooleanMethod(secClass, secMid, o_content_resolver, jStringParam, env->NewStringUTF("1"));

    env->DeleteLocalRef(jStringParam);

    return jStringKey;
}

But when I try to execute, I get an error.

E/stlibsettingsv: JNI ERROR (app bug): attempt to use stale Local 0x31 (should be 0x39)
A/stlibsettingsv: java_vm_ext.cc:542] JNI DETECTED ERROR IN APPLICATION: use of deleted local reference 0x31
    java_vm_ext.cc:542]     from boolean com.example.woodman.testlibsettingsv2.frmMain.SetSystemKeysString(java.lang.String)
    java_vm_ext.cc:542] "main" prio=5 tid=1 Runnable
A/stlibsettingsv: java_vm_ext.cc:542]   | group="main" sCount=0 dsCount=0 flags=0 obj=0x7968c450 self=0x74e3014c00
    java_vm_ext.cc:542]   | sysTid=32691 nice=-10 cgrp=default sched=0/0 handle=0x7568948548
    java_vm_ext.cc:542]   | state=R schedstat=( 316288736 11800419 156 ) utm=29 stm=2 core=5 HZ=100
    java_vm_ext.cc:542]   | stack=0x7fe34db000-0x7fe34dd000 stackSize=8MB
    java_vm_ext.cc:542]   | held mutexes= "mutator lock"(shared held)
    java_vm_ext.cc:542]   native: #00 pc 00000000003c26c0  /system/lib64/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, int, BacktraceMap*, char const*, art::ArtMethod*, void*, bool)+220)

Can everybody have a problem?

I read a lot of forums and websites. There is no solution anywhere. Thanks.

Add: This line causes an error.

jboolean jStringKey = env->CallStaticBooleanMethod(secClass, secMid, o_content_resolver, jStringParam, env->NewStringUTF("1"));

All the rest work out correctly. The problem with calling the putString method.

The culprit of the failure is this line:

jmethodID secMid = env->GetStaticMethodID(secClass, "putString", "(Landroid/content/ContentResolver;Landroid/net/Uri;Ljava/lang/String;Ljava/lang/String;)Z");

You are trying to find the static method ID of

protected static boolean putString(ContentResolver resolver, Uri uri, String name, String value)

for class android.provider.Settings$System , but this class does not have such a method at all.

Although, we know

public static final class System extends NameValueTable {
    ...
}

But, this is a static method of class NameValueTable which CANNOT be inherited by System class.

So, you need change the method signature to below:

jmethodID secMid = env->GetStaticMethodID(secClass, "putString", "(Landroid/content/ContentResolver;Ljava/lang/String;Ljava/lang/String;)Z");

Note that the ;Landroid/net/Uri is removed from your method signature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM