android开发入门教程

android开发入门教程
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

error: 'jstringToString' was not declared in this scope

Posted on 2014-10-23 16:04  android开发入门教程  阅读(1096)  评论(0)    收藏  举报

============问题描述============


刚接触写个jni代码,出现一个问题“error: 'jstringToString' was not declared in this scope”
请问这是为嘛呢?不胜感激..

错误如下:
[armeabi] Compile++ thumb: Inotify <= Inotify.cpp
jni/Inotify.cpp: In function 'void Java_lab_sodino_inotifylab_MainActivity_watch(JNIEnv*, jobject, jstring)':
jni/Inotify.cpp:16:49: error: 'jstringToString' was not declared in this scope
make: *** [obj/local/armeabi/objs/Inotify/Inotify.o] Error 1

代码如下:

#include <jni.h>

#include <string.h>

#include <android/log.h>



#define TAG "ANDROID_LAB" // 这个是自定义的LOG的标识

#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG ,__VA_ARGS__) // 定义LOGD类型





#ifdef __cplusplus

extern "C" {

#endif



JNIEXPORT void JNICALL Java_lab_sodino_inotifylab_MainActivity_watch

	(JNIEnv * env, jobject thiz, jstring pathWatch){

	char *char_pathWatch = NULL;

	char_pathWatch = jstringToString(env, pathWatch);

 	LOGD("watch path=%s", char_pathWatch);

}



char* jstringToString(JNIEnv* env, jstring jstr) {

	char* rtn = NULL;

	jclass clsstring = env->FindClass("java/lang/String");

	jstring strencode = env->NewStringUTF("utf-8");

	jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");

	jbyteArray barr = (jbyteArray) env->CallObjectMethod(jstr, mid, strencode);

	jsize array_lenth = env->GetArrayLength(barr);

	jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);

	if (array_lenth > 0) {

		rtn = (char*) malloc(array_lenth + 1);



		memcpy(rtn, ba, array_lenth);

		rtn[array_lenth] = 0;

	}

	env->ReleaseByteArrayElements(barr, ba, 0);

	return rtn;

}



#ifdef __cplusplus

}

#endif



============解决方案1============


char* jstringToString(JNIEnv* env, jstring jstr) 没有提前声明吧,把这个函数移到 main 的前面去呢

============解决方案2============


char* jstringToString(JNIEnv* env, jstring jstr); 在最上面生明一下

============解决方案3============


主要是 c++编译的问题,编译器需要提前知道有哪些方法可以被调用,所以你可以选择提供函数声明,或者将函数提前到调用之前,否则编译器在编译到这里的时候,他发现没有对应的方法,就会报错的

具体信息你可以搜一下 c++ 的函数声明
我要啦免费统计