C ++返回HashMap< string,boolean>反对Java [英] C++ returning HashMap<string, boolean> object to Java
问题描述
我有一个JAVA调用的JNI函数,该函数需要构建并返回HashMap.映射的键为字符串",相应的值为布尔值"或布尔值"(可以使用,只要可行即可).使用下面的当前代码,该字符串已成功添加到返回的地图中,并且可以使用Java进行访问.但是,当尝试访问JAVA中的值时,该值将为null.
I have a JNI function that JAVA calls that needs to build and return a HashMap. The key for the map is 'String' and the respective value is 'boolean' or 'Boolean' (either one is fine, as long as it works). With the current code that I have (below), the string is successfully added to the map that is returned and can be accessed in Java. However, when trying to access the value in JAVA, it comes up null.
jclass mapclass = env->FindClass("java/util/HashMap");
jmethodID initmeth = env->GetMethodID(mapclass, "<init>", "()V");
jmethodID putmeth = env->GetMethodID(mapclass, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
jobject roster_return = env->NewObject(mapclass, initmeth);
int roster_map_size;
std::map<std::string, RosterItem>* roster_map = GetRosterMap();
std::map<std::string, RosterItem>::iterator iter;
if (!roster_map || roster_map->size() < 1)
return roster_return;
iter = roster_map->begin();
while (iter != roster_map->end())
{
env->CallObjectMethod(roster_return, putmeth, env->NewStringUTF(iter->second.name.c_str()), (jboolean)iter->second.isfriend);
iter++;
}
我尝试生成一个布尔对象,但似乎无法弄清楚如何创建一个新的对象.我尝试了以下代码,但是布尔"init"的"GetMethodID"错误.
I've tried generating a Boolean object, but I cannot seem to figure out how to create a new one. I've tried the following code, but it errors on the "GetMethodID" for the boolean "init".
jclass mapclass = env->FindClass("java/util/HashMap");
jclass boolclass = env->FindClass("java/lang/Boolean");
jmethodID initmeth = env->GetMethodID(mapclass, "<init>", "()V");
//-----------------It errors on the next line-----------------------
jmethodID initbool = env->GetMethodID(boolclass, "<init>", "()V");
jmethodID putmeth = env->GetMethodID(mapclass, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
jobject roster_return = env->NewObject(mapclass, initmeth);
int roster_map_size;
std::map<std::string, RosterItem>* roster_map = GetRosterMap();;
std::map<std::string, RosterItem>::iterator iter;
if (!roster_map || roster_map->size() < 1)
return roster_return;
iter = roster_map->begin();
while (iter != roster_map->end())
{
LOGE("adding contact: %s", iter->second.jid.Str().c_str());
//---Not sure what to pass in the next function here for the fourth argument---
env->CallObjectMethod(roster_return, putmeth, env->NewStringUTF(iter->second.name.c_str()), (jboolean)iter->second.isfriend);
iter++;
}
推荐答案
旧问题,但今天我也在搜索此问题.当您在两种语言之间跳来跳去时,很容易忘记Java的原始boolean
类型与可为空的Boolean
对象类型之间存在差异.映射的值必须是一个对象,因此需要Boolean
.
Old question, but I was searching for this today as well. When you're bouncing around between languages it's easy to forget that there's a difference between Java's primitive boolean
type vs the nullable Boolean
Object type. A Map's value has to be an Object, thus Boolean
is needed.
因此,要创建一个新的Boolean
对象:
So, to create a new Boolean
object:
// Sample C++ boolean variable you want to convert to Java:
bool someBoolValue = true;
// Get the Boolean class
jclass boolClass = env->FindClass("java/lang/Boolean");
// Find the constructor that takes a boolean (note not Boolean) parameter:
jmethodID boolInitMethod = env->GetMethodID(boolClass, "<init>", "(Z)V");
// Create the object
jobject boolObj = env->NewObject(boolClass,
boolInitMethod,
someBoolValue ? JNI_TRUE : JNI_FALSE);
这篇关于C ++返回HashMap< string,boolean>反对Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!