I create a function to convert Map to jobject in JNI
and send this jobject to JAVA...
it's fine to boolean, but string and integer will crash....
Why does process crash at string and integer?
typedef Map<std::string, AbObj> Dictionary;
jobject Dictionary2jobject(JNIEnv *env, Dictionary* pDictionary)
{
jclass class_Hashtable=env->FindClass("java/util/Hashtable");
jmethodID construct_method=env->GetMethodID( class_Hashtable, "<init>","()V");
jobject objMap = env->NewObject(class_Hashtable, construct_method, "");
jmethodID add_method= env->GetMethodID(class_Hashtable,"put","(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
for(Dictionary::const_iterator it = pDictionary->begin(); it != pDictionary->end(); ++it)
{
switch(it->second.GetType())
{
case AbObj::OT_NIL:
return NULL;
case AbObj::OT_STR:
{
env->CallObjectMethod(objMap, add_method, env->NewStringUTF(it->first.c_str()), env->NewStringUTF(it->second.data._str));
}
break;
case AbObj::OT_BLN:
{
env->CallObjectMethod(objMap, add_method, env->NewStringUTF(it->first.c_str()), (jboolean)it->second.data._bln);
}
break;
case AbObj::OT_S32: //int32_t
{
env->CallObjectMethod(objMap, add_method, env->NewStringUTF(it->first.c_str()), (jint)999);
}
break;
case AbObj::OT_S64: //int64_t
{
env->CallObjectMethod(objMap, add_method, env->NewStringUTF(it->first.c_str()), (jdouble)it->second.data._s64);
}
break;
}
}
return objMap;
}
0 comments:
Post a Comment