我怎样才能设置和QUOT值;双"通过JNI输入我的类变量?我的、变量、QUOT、JNI

2023-09-06 23:45:55 作者:亦或是旅途风光

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

 公共静态本地INT getDoubleVar(双人间DOBJ);JNIEXPORT jint JNICALLtest_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 =价值;    }} 
android studio怎么引入.so文件

本机:

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

C codeS:

  JNIEXPORT jint JNICALLtest_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); 

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

解决方案

我得到正确的答案:

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->的findClass(ENV,爪哇/郎/整型);   jmethodID initInt = env->的GetMethodID(ENV,intClass,&所述;初始化>中,(I)的V);    如果(NULL == initInt)返回-1;    jobject newIntObj = env-> NewObject的(环境,intClass,initInt,123);//现在你们整成价值atttribute。对于这一点,我会//建议你有一个Java setter和调用它以同样的方式//如上所示//干净的参考env-> 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..