查看原文:http://newfaction.net/2010/12/10/android-jni-fieldid-and-methodid-usage.html
FieldID 用法
static void setTitle(JNIEnv* env, jobject thiz, jstring str)
{
char buf[256];
const char *strUTF = env->GetStringUTFChars(str, 0);
snprintf(buf, sizeof(buf), "venus set Title: %s\n", strUTF);
env->ReleaseStringUTFChars(str, strUTF);
jstring title = env->NewStringUTF(buf);
jclass cls = env->GetObjectClass(thiz);
jfieldID fid;
fid = env->GetFieldID(cls, "mTitle", "Ljava/lang/String;");
env->SetObjectField(thiz, fid, title);
env->ReleaseStringUTFChars(title, buf);
LOGD("--------------setTitle------------>>>>%s\n", buf);
}
MethodID 用法
static void setTitle(JNIEnv* env, jobject thiz, jstring str)
{
char buf[256];
const char *strUTF = env->GetStringUTFChars(str, 0);
snprintf(buf, sizeof(buf), "venus set Title: %s\n", strUTF);
env->ReleaseStringUTFChars(str, strUTF);
jstring title = env->NewStringUTF(buf);
jclass cls = env->GetObjectClass(thiz);
jmethodID mid;
mid = env->GetMethodID(cls, "setTitle", "(Ljava/lang/String;)V");
env->CallVoidMethod(thiz, mid, title);
env->ReleaseStringUTFChars(title, buf);
LOGD("--------------setTitle------------>>>>%s\n", buf);
}