简体   繁体   English

Android-将JNI NewObject()传递给Java时不保存值

[英]Android - JNI NewObject() does not save values when passing it to 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调用时,该对象似乎具有空值。

Here's the code of the java object I create in native: 这是我在本机中创建的Java对象的代码:

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

}

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. 这是用于创建类实例并将其传递给Java方法“ onGetTeam”的本机代码,该方法将上述类的实例作为参数。 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");
                }
            }

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. 我希望它在对Java的调用期间有效,然后可以对其进行垃圾回收。 However its data fields - that are set in the constructor - are just null when I call it, Why? 但是,当我将其数据字段(在构造函数中设置)设置为null时,为什么呢?

You're not passing the object you constructed to the method (or indeed doing anything with it). 您没有将构造的对象传递给方法(或实际上对它执行任何操作)。

Instead of 代替

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

don't you want 你不想要

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

?

Also you're not checking whether that call succeeded or not. 另外,您不检查该呼叫是否成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM