简体   繁体   中英

How to return QAndroidJniObject from a native function?

I'm trying to return QAndroidJniObject from a native function. Using Qt 5.5 and QtAndroidExtras. This is my first naive implementation:

QAndroidJniObject doSomething() {
  return QAndroidJniObject("android/os/Bundle", "()V");
}

static jobject nativeFunc(JNIEnv *env, jclass, jlong nativePointer, int, jstring, jstring, jstring)
{
  QAndroidJniObject obj = doSomething();                                       
  return obj.object();
}

Unfortunately, I'm getting an error:

I/dalvikvm(32413): JNI ERROR (app bug): accessed deleted global reference 0x1de00462

When QAndroidJniObject is destroyed, the underlying java object is also destroyed. It seems that QAndroidJniObject does not have a detach() method or something.

通过创建新的本地引用解决了此问题:

return env->NewLocalRef(obj.object<jobject>());

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