简体   繁体   English

“ this”在Android JNI中不是有效的JNI参考

[英]“this” is not a valid JNI reference in Android JNI

I am passing the current activity to the native method using Java Native Interface on android. 我正在使用android上的Java本机接口将当前活动传递给本机方法。 But I am not doing it using JNI like function names. 但是我没有使用像函数名这样的JNI来做到这一点。 I am registering native functions manually. 我正在手动注册本机函数。

This works (JNI naming). 有效 (JNI命名)。

com_venkatesh_home.c com_venkatesh_home.c

JNIEXPORT void JNICALL Java_com_venkatesh_Home_doStuff(JNIEnv *env, jobject activity) {
    jclass Activity = (*env)->GetObjectClass (env, activity);

com.venkatesh.Home.java com.venkatesh.Home.java

private native void doStuff();
static {
    System.loadLibrary("venkatesh");
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    doStuff();
 }


But this does not work . 但这是行不通的 (Manual registration and passing activity as an object) (以手动登录和通过活动为对象)

me.c

static JavaVM *java_vm;

void do_stuff (jobject activity)
{
    JNIEnv *env;
    if ((*java_vm)->GetEnv(java_vm, (void **) &env, JNI_VERSION_1_6) != JNI_OK) {
        LOG_D("GetEnv failed");
        return -1;
    }

    jclass Activity = (*env)->GetObjectClass (env, activity);
}

jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
    java_vm = vm;

    JNIEnv* env;
    if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6) != JNI_OK) {
        LOG_D ("GetEnv failed.");
        return -1;
    }

    // Find the class calling native function
    jclass Home = (*env)->FindClass(env, "com/venkatesh/Home");
    if (Home == NULL) {
        LOG_D ("FindClass failed : No class found.");
        return -1;
    }

    // Register native method for getUsbPermission
    JNINativeMethod nm[1] = {
        { "doStuff", "(Landroid/app/Activity;)V", do_stuff}
    };

    if ((*env)->RegisterNatives(env, NativeUsb, nm , 1)) {
         LOG_D ("RegisterNatives Failed.");
         return -1;
    }

    return JNI_VERSION_1_6;
}

com.venkatesh.Home.java com.venkatesh.Home.java

private native void doStuff(Activity activity);
static {
    System.loadLibrary("venkatesh");
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    doStuff(this);
 }

The error is 错误是

JNI WARNING: 0xb89a7788 is not a valid JNI reference (GetObjectClass)

where 0xb89a7788 is the "this" received on jni side as activity. 其中0xb89a7788是在jni端作为活动收到的“ this”。

Now, as I understand this represents the present object. 现在,据我了解,这代表了当前的目标。 Equivalent to self in python. 相当于python中的self。 But then I am passing an object to the native side and hence it should be a valid reference. 但是随后我将一个对象传递给本机端,因此它应该是有效的引用。 Why the invalid reference error? 为什么无效的参考错误? It this not an object? 这不是对象吗? What is wrong? 怎么了?

This is happening not because of how the method is being registered, but because of the signature of your native method. 发生这种情况的原因并不在于方法的注册方式,而在于您本机方法的签名。 I would rewrite that method like this: 我将这样重写该方法:

void do_stuff (JNIEnv *env, jobject this, jobject activity)
{
    jclass Activity = (*env)->GetObjectClass (env, activity);
    // ...and whatever else you want...
}

Every JNI method must take a JNIEnv * as its first parameter. 每个JNI方法都必须将JNIEnv *作为其第一个参数。 (Note also the lack of the static env variable - it isn't needed). (还要注意缺少static env变量-不需要)。 Also, since it is not a static method, the first parameter passed in to the method will be this and not activity . 另外,由于它不是静态方法,因此传入该方法的第一个参数将是this而不是activity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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