如何将 int 放入 HashMap jni 中?

我有这样的方法


jobject Profiling::getFullStatistic(JNIEnv *env)

{

    env->PushLocalFrame(256); // fix for local references


    jclass mapClass = env->FindClass("java/util/HashMap");


    if (mapClass == nullptr)

    {

        return nullptr;

    }


    jmethodID init = env->GetMethodID(mapClass, "<init>", "(I)V");

    jobject hashMap = env->NewObject(mapClass, init, m_values.size());

    jmethodID put = env->GetMethodID(mapClass, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");


    for (auto &it: m_values)

    {

        env->CallObjectMethod(hashMap, put, env->NewStringUTF(it.first.c_str()), env->NewStringUTF(std::to_string(it.second).c_str()));

    }


    return env->PopLocalFrame(hashMap);

}

最后我得到了Map<String, String>,现在我想将此 Map 类型更改为Map<String, Integer>


所以,我只是将方法的签名更改put为此


jmethodID put = env->GetMethodID(mapClass, "put", "(Ljava/lang/Object;I;)Ljava/lang/Object;");

据我所知int签名是I...


而且我也改变了这一行


env->CallObjectMethod(hashMap, put, env->NewStringUTF(it.first.c_str()), it.second);

但是知道我有这样的错误


JNI DETECTED ERROR IN APPLICATION: JNI NewStringUTF called with pending 

exception java.lang.NoSuchMethodError: no non-static method 

"Ljava/util/HashMap;.put(Ljava/lang/Object;I;)Ljava/lang/Object;"

at java.util.Map com..ar.JniInterface.getProfilingStatistic(long) 

(JniInterface.java:-2)

at void com..ar.ui.fragments.ar.ArScreen.lambda$testDeleteIt$1$ArScreen() 

(ArScreen.java:156)

at void 

com..ar.ui.fragments.ar.-$$Lambda$ArScreen$zqdXNYFzVzmF_ZYFKJRM26bZzUw.run() 

(lambda:-1)

at void android.os.Handler.handleCallback(android.os.Message) 

(Handler.java:873)

at void android.os.Handler.dispatchMessage(android.os.Message) 

(Handler.java:99)

at void android.os.Looper.loop() (Looper.java:214)

at void android.app.ActivityThread.main(java.lang.String[]) 

(ActivityThread.java:6990)

at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, 

java.lang.Object[]) (Method.java:-2)

at void com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run() 


元芳怎么了
浏览 138回答 1
1回答

梵蒂冈之花

从 C++调用Integer#valueOf(int)并将结果传递给HashMap#put(Object, Object):jclass j_Integer = env->FindClass("java/lang/Integer");jmethodID j_Integer_valueOf = env->GetStaticMethodID(j_Integer, "valueOf", "(I)Ljava/lang/Integer;");jmethodID put = env->GetMethodID(mapClass, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");for (auto &it: m_values) {&nbsp; &nbsp; jobject j_intValue = env->CallStaticObjectMethod(j_Integer, j_Integer_valueOf, it.second);&nbsp; &nbsp; env->CallObjectMethod(hashMap, put, env->NewStringUTF(it.first.c_str()), j_intValue);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java