Android的 - NewObject的JNI()将它传递到Java时不保存值 [英] Android - JNI NewObject() does not save values when passing it to Java

查看:124
本文介绍了Android的 - NewObject的JNI()将它传递到Java时不保存值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用本地code创建一个对象,然后我作为参数传递给我打电话的方法。
好像没有任何出错的本土code,但是当我在Java中调用,对象似乎有调零值。

I am using native code to create an object which I then pass as an argument to a method I call. Nothing appears to go wrong in native code, but when I get the call in Java, the object seems to have nulled values.

这里的java对象的code我本地创建:

Here's the code of the java object I create in native:

package org.test;
import java.util.ArrayList;

public class JNITeam {
    public int mTeamID;
    public String mTeamName;
    private ArrayList<String> mMembers;

    public JNITeam(int id, String name) {
        mTeamID = id;
        mTeamName = name;
    }

    public void addMember(String name) {
        mMembers.add(name);
    }

}

下面是本机code用来创建类的实例,并把它传递到Java方法onGetTeam,这需要上面的类的实例作为参数。它是从天然code创建的线程运行,因此,我必须附上线程。

Here's the native code used to create an instance of the class and pass it up to the Java method "onGetTeam", which takes an instance of the above class as a parameter. It is run from a thread created in Native code, hence I have to attach the thread.

JNIEnv* jenv = 0;
            clientHandle->runningJVM->AttachCurrentThread(&jenv,0);
            if (!jenv)
                __android_log_print(ANDROID_LOG_INFO, ANDROID_DEBUG_TAG, "jenv is null");

            jclass cls = jenv->GetObjectClass(clientHandle->job);
            if (!cls)
                __android_log_print(ANDROID_LOG_INFO, ANDROID_DEBUG_TAG, "cls is null");

            jmethodID constructor = jenv->GetMethodID(clientHandle->JNITeamCls, "<init>", "(ILjava/lang/String;)V");

            jint teamID = 2;
            jstring js = jenv->NewStringUTF("test");

            jobject dataObject = jenv->NewObject(clientHandle->JNITeamCls, constructor, teamID, js);
            if (!dataObject)
                __android_log_print(ANDROID_LOG_INFO, ANDROID_DEBUG_TAG, "dataobject is null");

            if (jenv && cls && dataObject) {
                jmethodID mid = jenv->GetMethodID(cls,"onGetTeam","(Lorg/test/JNITeam;)V");
                if (mid) {
                    jenv->CallVoidMethod(clientHandle->job,mid);
                }
                else {
                    __android_log_print(ANDROID_LOG_INFO, ANDROID_DEBUG_TAG, "mid is null");
                }
            }

我不希望对象是持久的;我希望它ONY调用Java的过程中是有效的,那么它可以被垃圾回收。但是它的数据字段 - 即在构造函数中设置 - 只是空当我打电话了,为什么

I do not want the object to be persistent; I want it to ony be valid during the call to Java, then it can be garbage-collected. However its data fields - that are set in the constructor - are just null when I call it, Why?

推荐答案

你不及格你构造方法的对象(或者实际上与做任何事情)。

You're not passing the object you constructed to the method (or indeed doing anything with it).

而不是

                jenv->CallVoidMethod(clientHandle->job,mid);

你不想

                jenv->CallVoidMethod(clientHandle->job,mid,dataObject);

另外你不检查调用是否成功与否。

Also you're not checking whether that call succeeded or not.

这篇关于Android的 - NewObject的JNI()将它传递到Java时不保存值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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