我怎样才能设置和QUOT值;双"通过JNI输入我的类变量? [英] How can I set the value of "Double" type variable of my Class by JNI?

查看:137
本文介绍了我怎样才能设置和QUOT值;双"通过JNI输入我的类变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我只是想设置值double类型的变量,我可以code,如:

 公共静态本地INT getDoubleVar(双人间DOBJ);
JNIEXPORT jint JNICALL
test_jni_Native_testGet(JNIEnv的* ENV,JCLASS类型,jobject DOBJ)
{
    JCLASS DoubleClass = env->的findClass(爪哇/郎/双人间);
    jfieldID VALUEID = env-> GetFieldID(DoubleClass,价值,D);
    env-> SetDoubleField(DOBJ,VALUEID,2.3);
    返回0;
}

这些codeS托克效果。

但是,当我试图设置由JNI类的双师型变量的值,我无法得到我想要的东西,当然,它打破了,这让我有点糊涂了。

Java的codeS:

 公共类TestDouble
{    公共双值;
    众长NUM;
    公共TestDouble(NUM长双值)
    {
        this.num = NUM​​;
        THIS.VALUE =价值;
    }
}

本机:

 公共静态本地INT testGet(TestDouble tdobj);

C codeS:

  JNIEXPORT jint JNICALL
test_jni_Native_testGet(JNIEnv的* ENV,JCLASS类型,jobject tdobj)
{
    JCLASS tdobjClass = env->的findClass(XXXX / TestDouble);
    jfieldID VALUEID = env-> GetFieldID(tdobjClass,价值,D);
    env-> SetDoubleField(tdobj,jValueID,2.3);
    返回0;
}

我想设置类的TestDouble的价值和价值型的值是双(而不是增加一倍)。

  E / dalvikvm:VM中止
致命的信号6
发送停止信号,PID:5830空隙debuggerd_signal_handler(INT,siginfo_t *,无效*)

我刚刚粘贴的关键错误的话这里,而且很明显,发生错误的:

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

我能做些什么来解决这个问题呢?
非常感谢!


解决方案

我得到正确的答案:

<$p$p><$c$c>http://stackoverflow.com/questions/34848605/how-to-set-the-value-of-a-double-integer-type-of-a-java-class-by-jniJNIEXPORT jint JNICALL test_jni_Native_testSet(JNIEnv的* ENV,JCLASS类型,jobject tdobj)
{
    //创建Integer类,得到的构造和创建Integer对象
    JCLASS intClass = env-&GT;的findClass(ENV,爪哇/郎/整型);
   jmethodID initInt = env-&GT;的GetMethodID(ENV,intClass,&所述;初始化&gt;中,(I)的V);
    如果(NULL == initInt)返回-1;
    jobject newIntObj = env-&GT; NewObject的(环境,intClass,initInt,123);//现在你们整成价值atttribute。对于这一点,我会
//建议你有一个Java setter和调用它以同样的方式
//如上所示//干净的参考
env-&GT; DeleteLocalRef(ENV,newIntObj);
返回0;
}

整数/双和其他类型的包装可以用相同的方式处理。

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;
}

These codes toke effect.

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 codes:

public class TestDouble
{

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

Native:

public static native int testGet(TestDouble tdobj);

c codes:

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;
}

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 !

解决方案

I get the correct answer :

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 and other wrapper types can be handled in the same way..

这篇关于我怎样才能设置和QUOT值;双&QUOT;通过JNI输入我的类变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆