简体   繁体   English

如何使用自定义Object类为JNI方法定义返回类型?

[英]How to define a return type for a JNI method with a custom Object class?

I have a method in JNI (C++) and I want to be able to return a custom Object type (not a primitive or a String etc...) I've written something down but I keep getting java.lang.UnsatisfiedLinkError error. 我在JNI(C ++)中有一个方法,我希望能够返回一个自定义的Object类型(不是一个原语或一个String等...)我写了一些东西,但我一直得到java.lang.UnsatisfiedLinkError错误。

Here are the details: 以下是详细信息:

The method: 方法:

static jobject Java_android_sdk_Core_ProcessFrame(JNIEnv *env, jobject myobj, jbyteArray frameData)
    {

     jclass clazz;
     jmethodID cid;
     jobject jCoreOut;
     static const char* const strClassName = "android/sdk/Core/CoreOutput";
     clazz = env->FindClass(strClassName);
     if (clazz == NULL) {
      LOGE("Can't find class CoreOutput");

     }
     cid = env->GetMethodID(clazz,"<init>", "()V");
     jCoreOut = env->NewObject(clazz, cid);


     // Free local references 
        env->DeleteLocalRef(clazz);

     return jCoreOut;

    }

I have the methods descriptors defined in the following way: 我有以下列方式定义的方法描述符:

    static const JNINativeMethod gMethods[] = {
        { "CreateCore", "(III)V", (void*) Java_android_sdk_Core_CreateCore },
  { "ProcessFrame", "([B)Landroid/sdk/Core/CoreOutput;", (void*) Java_android_sdk_Core_ProcessFrame }
 };

I'm performing the method registration by calling on: 我正在通过调用以下方式执行方法注册:

     if (env->RegisterNatives(clazz, gMethods,
    sizeof(gMethods) / sizeof(gMethods[0])) != JNI_OK)
 {
  LOGE("Failed registering JNI methods");
  return result;
 }

And the registration for my other native methods is successful. 我的其他原生方法的注册是成功的。 (I'm able to use them...) (我能用它们......)

When I try to invoke the ProcessFrame method I get the following output from Logcat: 当我尝试调用ProcessFrame方法时,我从Logcat获得以下输出:

11-23 16:10:10.139: ERROR/AndroidRuntime(4923): java.lang.UnsatisfiedLinkError: ProcessFrame

I'm sure it has something to do with me not defining the method correctly. 我确定这与我没有正确定义方法有关。 Can anyone shed some light on this? 任何人都可以对此有所了解吗?

Can anyone point me to a good tutorial which covers more than handling of primitive types in JNI? 任何人都可以指出一个很好的教程,其中涵盖的不仅仅是处理JNI中的原始类型吗?

Thank you, 谢谢,

Itamar 伊塔马尔

UnsatisfiedLinkError is thrown when the JVM cant find the method. 当JVM找不到方法时,抛出UnsatisfiedLinkError。 So it has to do with your method declaration. 所以它与您的方法声明有关。 Look into javah for creating the function header for you. 查看javah为您创建函数头。 Look into this: javah 看看这个: javah

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

相关问题 如何定义泛型类的JNI方法签名? - How do I define the JNI method signature of a generic class? 如何定义方法返回的结构类型 - How define Structural Type that the method return this JAva:如何将自定义对象类型作为参数传递给方法,并返回相同的自定义对象类型值 - JAva:How to pass a custom object type as a parameter to a method and return the same custom object type value back 如何返回类 - 在方法中键入 - How to return class - type in a method 从jni返回并访问Java对象/类 - Return and access java object/class from jni 如何定义将对象作为参数并将其返回到另一个对象的方法? - How to define a method that would take an object as a parameter and return that into another object? 如何用每个对象的类的返回类型声明一个类型间方法? - How to declare an inter-type method with return type of every object's class? 如何从适用于多种 Class 类型的方法返回实际类型而不是 Object 类型? - How to return the actual type rather than an Object type from a method that can work for multiple Class types? 如何使用自引用类型返回值定义方法 - How to define method with self-referencing Type return value 如何在Java泛型方法中定义返回类型? - How do I define the return type inside a Java generics method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM