---------------------------------------华丽的分割线-----------------------------------------
现象:“.c”后缀名改为“.cpp”时java调用native失败 。
解决: 加入“ extern "C" ”。
现象:“error: base operand of '->' has non-pointer type '_JNIEnv'”错误。
解决: 将“(*env)->NewStringUTF(env, "HelloWorld from JNI !");”改为“env->NewStringUTF("HelloWorld from JNI !")”。
例子:
#include <stdio.h>
#include <string.h>
#include <android/log.h>
#include <jni.h>
#ifdef __cplusplus
extern "C"
{
#endif
jint Java_com_duicky_MainActivity_add(JNIEnv* env, jobject thiz, jint x, jint y)
{
//该方法为打印的方法
__android_log_print(ANDROID_LOG_INFO, "JNIMsg", "Get Param: x=%d y=%d ", x, y);
int iRet = x + y;
return iRet;
}
jstring Java_com_duicky_MainActivity_getString(JNIEnv* env, jobject thiz)
{
jstring strRet = env->NewStringUTF("HelloWorld from JNI !");
return strRet;
}
#ifdef __cplusplus
}
#endif
本文详细介绍了在使用Java调用native方法时遇到的两个常见问题:'.c'后缀名改为'.cpp'时的java调用native失败,以及'error: base operand of '->' has non-pointer type '_JNIEnv'的错误,并提供了相应的解决方案。通过实例代码,读者可以清晰地理解如何避免和解决这些问题。
713

被折叠的 条评论
为什么被折叠?



