2023-01-20 337
我有一个JNI函数,JAVA调用需要构建和返回哈希图.地图的关键是”字符串”,相应的值为”布尔值”或”布尔值”(只要有效,要么可以).使用我拥有的当前代码(下面),将字符串成功添加到返回的地图中,可以在Java中访问.但是,当试图访问Java中的值时,它会出现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”上出现了错误.
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类型与Nullable Boolean对象类型之间存在区别.地图的值必须是一个对象,因此需要Boolean.
so,创建一个新的Boolean对象:
// 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);
也许如果您在Java中定义静态函数createMap()和addTomap(字符串,布尔值),并且只需根据需要从JNI调用它们,而不是经历所有获取正确的类和正确的类,和仅在JNI中的字段.
以上所述是小编给大家介绍的C ++返回哈希玛普<字符串,布尔值>对java的对象,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/26051.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态