如何设置“Double"的值?JNI 的我的类的类型变量?

How can I set the value of quot;Doublequot; type variable of my Class by JNI?(如何设置“Double的值?JNI 的我的类的类型变量?)

本文介绍了如何设置“Double"的值?JNI 的我的类的类型变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我只想将值设置为 Double 类型的变量,我可以这样编写代码:

If I just want to set value to Double type variable, I may code like:

public static native int getDoubleVar(Double dobj);
JNIEXPORT jint JNICALL
test_jni_Native_testGet(JNIEnv *env, jclass type, jobject dobj)
{
    jclass DoubleClass = env->FindClass("java/lang/Double");
    jfieldID valueID = env->GetFieldID(DoubleClass, "value", "D");
    env->SetDoubleField(dobj, valueID, 2.3);
    return 0;
}

这些代码生效.

但是,当我试图通过 JNI 设置一个类的Double"变量的值时,我无法得到我想要的,当然,它崩溃了,这让我有点困惑.

But, when I tried to set the value of a "Double" variable of a class by JNI, I can't get what I want, and of course, it broke down, which makes me a bit confused.

Java 代码:

public class TestDouble
{

    public Double value;
    public long num;
    public TestDouble(long num, Double value)
    {
        this.num = num;
        this.value = value;
    }
}

本地人:

public static native int testGet(TestDouble tdobj);

c 代码:

JNIEXPORT jint JNICALL
test_jni_Native_testGet(JNIEnv *env, jclass type, jobject tdobj)
{
    jclass tdobjClass = env->FindClass("xxxx/TestDouble");
    jfieldID valueID = env->GetFieldID(tdobjClass, "value", "D");
    env->SetDoubleField(tdobj, jValueID, 2.3);
    return 0;
}

我想设置类'TestDouble'的'value'的值,'value'的类型是Double"(不是double).

I'd like to set the value of 'value' of class 'TestDouble', and type of 'value' is "Double" (not double).

E/dalvikvm: VM aborting
Fatal signal 6
Send stop signal to pid:5830 in void debuggerd_signal_handler(int, siginfo_t*, void*)

我这里只是粘贴了关键的错误词,很明显错误发生在:

I just paste the key wrong words here, and it's obvious that the error occurs at:

env->SetDoubleField(tdobj, jValueID, 2.3);

那我该怎么做才能解决这个问题呢?非常感谢!

What can I do to deal with this problem then? Thanks a lot !

推荐答案

我答对了:

http://stackoverflow.com/questions/34848605/how-to-set-the-value-of-a-double-integer-type-of-a-java-class-by-jni

JNIEXPORT jint JNICALL test_jni_Native_testSet(JNIEnv *env, jclass type, jobject tdobj)
{
    //Create Integer class, get constructor and create Integer object
    jclass intClass = env->FindClass(env, "java/lang/Integer");
   jmethodID initInt = env->GetMethodID(env, intClass, "<init>", "(I)V");
    if (NULL == initInt) return -1;
    jobject newIntObj = env->NewObject(env, intClass, initInt, 123);

//Now set your integer into value atttribute. For this, I would
//recommend you to have a java setter and call it in the same way 
//as shown above

//clean reference
env->DeleteLocalRef(env, newIntObj); 
return 0;
}

Integer/Double 和其他包装类型可以用同样的方式处理..

Integer/Double and other wrapper types can be handled in the same way..

这篇关于如何设置“Double"的值?JNI 的我的类的类型变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何设置“Double"的值?JNI 的我的类的类型变量?

基础教程推荐