ITeye博客

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

hanyezhiyu8 · 2011-09-07 16:11 · Android问题与错误 · 移动开发 · Markdown

---------------------------------------华丽的分割线-----------------------------------------

现象:“.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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。