JNI jni / cyberlevel9.c:17:31:错误:请求成员'NewDirectByteBuffer'的东西不是结构或联合

时间:2013-07-04 19:29:58

标签: android c++ c eclipse java-native-interface

当我尝试构建时,Eclipse给了我这个错误:

jni/cyberlevel9.c:17:31: error: request for member 'NewDirectByteBuffer' in something  not a structure or union
jni/cyberlevel9.c:18:28: error: request for member 'NewGlobalRef' in something not a structure or union

这是代码的问题部分:

JNIEXPORT jobject JNICALL Java_com_cyberbg_natcamlevel9_NativeCameraLevel9Start_allocNativeBuffer(JNIEnv* env, jobject this, jlong size)
{
void* buffer = malloc(size);
jobject directBuffer = env->NewDirectByteBuffer(buffer, size);
jobject globalRef = env->NewGlobalRef(directBuffer);

return globalRef;
//return (NewDirectByteBuffer*)(*env)->NewDirectByteBuffer(buffer, size);
}

1 个答案:

答案 0 :(得分:6)

来自C程序的JNI调用看起来像

(*env)->fun(env, p1, ...)

您的调用样式在C ++中是可以的,其中jni.h中定义了一个特殊的包装类:

env->fun(p1, ...)

您只需将jni/cybrrlevel9.c重命名为jni/cyberlevel9.cpp

即可解决问题

或者,

jobject directBuffer = (*env)->NewDirectByteBuffer(env, buffer, size); 
jobject globalRef = (*env)->NewGlobalRef(env, directBuffer);