Jni使用过程中出现 error: request for member 'FindClass' in something not a structure or union,解决办法

原文: http://topic.csdn.net/u/20110120/10/ef601a64-27fa-4a80-96be-39dbcb644cbc.html

问题:

在android 里使用JNI,总是报错
packages/apps/SystemMointor/jni/proc_reader.c:78: error: request for member 'GetStringUTFChars' in something not a structure or union
packages/apps/SystemMointor/jni/proc_reader.c:92: error: request for member 'FindClass' in something not a structure or union
packages/apps/SystemMointor/jni/proc_reader.c:94: error: request for member 'NewObjectArray' in something not a structure or union
packages/apps/SystemMointor/jni/proc_reader.c:96: error: request for member 'FindClass' in something not a structure or union
packages/apps/SystemMointor/jni/proc_reader.c:98: error: request for member 'GetFieldID' in something not a structure or union
packages/apps/SystemMointor/jni/proc_reader.c:99: error: request for member 'GetFieldID' in something not a structure or union
packages/apps/SystemMointor/jni/proc_reader.c:108: error: request for member 'SetObjectField' in something not a structure or union
packages/apps/SystemMointor/jni/proc_reader.c:108: error: request for member 'NewStringUTF' in something not a structure or union
packages/apps/SystemMointor/jni/proc_reader.c:116: error: request for member 'SetObjectField' in something not a structure or union
packages/apps/SystemMointor/jni/proc_reader.c:118: error: request for member 'SetObjectArray' in something not a structure or union


解决办法:

问题解决了,原来是这样的:
如果是c程序,要用 (*env)->
如果是C++要用 env->

ps:在linux下如果.c文件中用 “env->” 编译会找不到此结构,必须用“(*env)->”,或者改成.cpp文件,以 c++的方式来编译。

以下是两者的区别:

jni.h中

struct JNINativeInterface_;

struct JNIEnv_;

#ifdef __cplusplus
typedef JNIEnv_ JNIEnv;
#else
typedef const struct JNINativeInterface_ *JNIEnv;
#endif

/*
* We use inlined functions for C++ so that programmers can write:
*
* env->FindClass("java/lang/String")
*
* in C++ rather than:
*
* (*env)->FindClass(env, "java/lang/String")
*
* in C.
*/

即C++中使用
env->FindClass("java/lang/String")


C中使用

(*env)->FindClass(env, "java/lang/String")

你可能感兴趣的:(Jni使用过程中出现 error: request for member 'FindClass' in something not a structure or union,解决办法)