如何将 cocos2d ValueMap 作为 HashMap 发送到 Java?

How to send cocos2d ValueMap to Java as HashMap?

我有 cocos2d:: ValueMap:

 ValueMap map;
 map["key1"] = "value1";
 map["key2"] = 1;
 map["key3"] = true;

我想将它作为 HashMap<String,Object>

发送到 Java

所以我在生成 jobject hashMapObj 的地方编写了以下代码,我想用值填充它:

jclass hashMapClass= jniGetInstance.env->FindClass("java/util/HashMap");
jmethodID hashMapInit = jniGetInstance.env->GetMethodID(hashMapClass, "<init>", "(I)V");
jobject hashMapObj = jniGetInstance.env->NewObject(hashMapClass, hashMapInit, values.size());

//javap -s -p java.util.HashMap  | grep -A 1 put\(
jmethodID hashMapId = jniGetInstance.env->GetMethodID(hashMapClass, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");

for (auto it : map) {   //https://github.com/cocos2d/cocos2d-x/blob/v3/cocos/base/CCValue.h
    switch (it.second.getType()) {
        case cocos2d::Value::Type::BOOLEAN:
            jniGetInstance.env->CallObjectMethod(hashMapObj, hashMapId, "put",
                                                 jniGetInstance.env->NewStringUTF(
                                                         it.first.c_str()),
                                                 it.second.asBool());
            break;
       //...

        default:
            break;
    }
}

然后我在排队时崩溃了:

jniGetInstance.env->CallObjectMethod(
   hashMapObj, 
   hashMapId,
   "put", 
   jniGetInstance.env->NewStringUTF(                                             
               it.first.c_str()),
               it.second.asBool());

JNI DETECTED ERROR IN APPLICATION: use of invalid jobject 0xd85d4aa0

编辑

jniGetInstance.env->CallObjectMethod(hashMapObj, hashMapId, "put","someString", true);

失败并出现同样的错误,所以问题出在 hashMapObj

有什么想法吗?

改变

jniGetInstance.env->CallObjectMethod(hashMapObj, hashMapId, "put",
    jniGetInstance.env->NewStringUTF(
        it.first.c_str()),
        it.second.asBool());

jniGetInstance.env->CallObjectMethod(hashMapObj, hashMapId,
    jniGetInstance.env->NewStringUTF(it.first.c_str()),
    it.second.asBool());

因为 "put" 方法只接受 2 个参数。

还要确保 it.second.asBool() returns jboolean - 它不同于 C++ bool 类型。