JNI:无法获取数组长度 [英] JNI: Can not get array length

查看:102
本文介绍了JNI:无法获取数组长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了下一个问题:我不能用C代码中的byte[] (jbyteArray)做任何事情. JNI中与数组一起使用的所有函数都导致JNI DETECTED ERROR IN APPLICATION: jarray argument has non-array type.我的代码有什么问题?

I faced with the next problem: I can not do anything with byte[] (jbyteArray) in C code. All functions that work with array in JNI cause JNI DETECTED ERROR IN APPLICATION: jarray argument has non-array type. What's wrong with my code?

C:

#include <stdio.h>
#include <jni.h>

static jstring convertToHex(JNIEnv* env, jbyteArray array) {
    int len = (*env)->GetArrayLength(env, array);// cause an error;
    return NULL;
}

static JNINativeMethod methods[] = {
    {"convertToHex", "([B)Ljava/lang/String;", (void*) convertToHex },
};

JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
    JNIEnv* env = NULL;

    if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {
        return -1;
    }

    jclass cls = (*env)->FindClass(env, "com/infomir/stalkertv/server/ServerUtil");

    (*env)->RegisterNatives(env, cls, methods, sizeof(methods)/sizeof(methods[0]) );

    return JNI_VERSION_1_4;
}

ServerUtil:

ServerUtil:

public class ServerUtil {

    public ServerUtil() {
        System.loadLibrary("shadow");
    }

    public native String convertToHex(byte[] array);
}

主要活动:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ServerUtil serverUtil = new ServerUtil();
        byte[] a = new byte[]{1,2,3,4,5};
        String s = serverUtil.convertToHex(a);
    }
}

我的环境:

  • Android Studio 2.0
  • 实验性Gradle插件0.7.0
  • JAVA 1.8
  • NDK r11b
  • Windows 10 x64

提前谢谢!

推荐答案

传递给函数的第二个参数不是 c2>.

The second argument passed to your function isn't a jbyteArray.

根据 JNI文档 ,传递给本机函数的参数为​​:

Per the JNI documentation, the arguments passed to a native function are:

本机方法参数

JNI接口指针是本机方法的第一个参数.这 JNI接口指针的类型为JNIEnv. 第二个参数不同 取决于本机方法是静态还是非静态.这 非静态本机方法的第二个参数是对 目的.静态本机方法的第二个参数是引用 到其Java类.

The JNI interface pointer is the first argument to native methods. The JNI interface pointer is of type JNIEnv. The second argument differs depending on whether the native method is static or nonstatic. The second argument to a nonstatic native method is a reference to the object. The second argument to a static native method is a reference to its Java class.

其余参数对应于常规Java方法参数. 本地方法调用将其结果传递回调用例程 通过返回值.

The remaining arguments correspond to regular Java method arguments. The native method call passes its result back to the calling routine via the return value.

您的jstring convertToHex(JNIEnv* env, jbyteArray array)缺少第二个jclassjobject自变量,因此您正在处理jobjectjclass自变量以及jbyteArray.

Your jstring convertToHex(JNIEnv* env, jbyteArray array) is missing the second jclass or jobject argument, so you're treating either a jobject or jclass argument and a jbyteArray.

这篇关于JNI:无法获取数组长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆