如何设置通过JNI双人间/整数类型的Java类的价值 [英] How to set the value of a Double/Integer type of a java class by JNI

查看:232
本文介绍了如何设置通过JNI双人间/整数类型的Java类的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早些时候,我问一个问题为:

Earlier, I ask a question as:

<一个href=\"http://stackoverflow.com/questions/34812975/how-can-i-set-the-value-of-double-type-variable-of-my-class-by-jni\">How可以设置我的类由JNI的双师型类型变量的值?

不过,我没有得到正确的答案是我想要的。
假设我有一个java类,如:

But, I didn't get the right answer what I want. Assume I had a java class like:

class Data
{
    public Integer value;
}

本机:

public static native int testGet(Data tdobj);

C codeS:

c codes:

JNIEXPORT jint JNICALL
test_jni_Native_testSet(JNIEnv *env, jclass type, jobject tdobj)
{
    jclass tdobjClass = env->FindClass("xxxx/Data");
    jfieldID valueID = env->GetFieldID(tdobjClass, "value", "Ljava/lang/Integer;");
    env->SetIntField(tdobj, jValueID, 123);
    return 0;
}

当我叫Native.testGet,程序出了故障,并显示埃罗消息如下:

And when I called Native.testGet, the program broke down, and show erro messages as follows:

E/dalvikvm: JNI ERROR (app bug): accessed stale weak global reference 0x7b (index 30 in a table of size 6)
E/dalvikvm: VM aborting
Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1), thread 26758 

我真的不知道

如何应对双/整数类型中的一类,这似乎是相同的字符串的,但它从字符串类型而有所不同。看起来,这种类型可以通过jobject处理,但结果让我感到困惑。

How to deal with Double/Integer types in a class, which seems like the same as String ones, but it differs from String type. It seems like that such types can be handled by jobject, but result makes me confused.

感谢。

我不希望使用基本类型如双/ INT达到我的目标,我不希望用功能。或者你可以告诉我,我希望的方式是不可能达到我的目标。非常感谢~~

I don't want to use a primitive type like double/int to reach my goal, and I don't want to use functions either. Or you may just tell me that, the way I wished is impossible to reach my goal. Thanks a lot~~

推荐答案

第一件事,当你得到数据字段例如,它没有初始化,因为你的java code表示,因此将需要创建一个实例。

First thing, when you get value field from Data instance, it is not initialized, as your java code shows, so would need to create an instance.

二,你不能改变整数值,你有你想要的值来实例化。因此,创建一个新的整数,然后从数据例如更改字段。事情是这样的:

Second, you can't change integer value, you have to instantiate it with the value you want. So, create a new Integer, and then change value field from Data instance. Something like this:

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

这篇关于如何设置通过JNI双人间/整数类型的Java类的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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