How can I set the value of quot;Doublequot; type variable of my Class by 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 的我的类的类型变量?


- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 获取数字的最后一位 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 转换 ldap 日期 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01