您的位置:首页 > 移动开发 > Android开发

Android将“.c”后缀名改为“.cpp”时java调用native失败及“error: base operand of '->' has non-poin[转]

2012-04-20 21:17 477 查看
现象:“.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 !")”。

例子:

Cpp代码



#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐