如何设置“Double”的值。我的类的类型变量由JNI? [英] How can I set the value of "Double" type variable of my Class by JNI?

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

问题描述

如果我只想将值设置为Double类型变量,我可以编写如下:

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

这些代码toke effect。



但是,当我试图通过JNI设置类的Double变量的值时,我不能得到我想要的,当然,它崩溃了,这使我有点困惑。 / p>

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'的类型是Double(不是double)。

  E / dalvikvm:VM aborting 
致命信号6
在void debuggerd_signal_handler(int,siginfo_t *,void *)中向pid:5830发送停止信号


b $ b

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

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

那么我该如何处理这个问题呢?
非常感谢!

解决方案

我得到正确的答案:

  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)
{
//创建整数类,获取构造函数并创建整数对象
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);

//现在将整数设置为值atttribute。为此,我会
//建议你有一个java安装程序,并以相同的方式调用
//如上所示

//清除引用
env-> DeleteLocalRef(env,newIntObj);
return 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..

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

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