如何将本地新创建的java类对象返回到java android?

发布于 2024-12-24 00:35:27 字数 11817 浏览 0 评论 0原文

我试图将本机中新创建的 java 类对象返回到 android。返回对象后,当我尝试访问返回对象的内部对象时,我面临崩溃。

到目前为止,我只是使用 NewObject() 函数,然后设置参数,然后从本机(c 语言)函数返回。

我是否需要使用任何其他函数来通知 JVM 并使该对象成为垃圾收集?

我已经在 eSnips 中上传了整个项目(Eclipse 项目)。 单击此处查看 Android 项目。这样您就可以下载并查看您的代码。

DataObject2.java 类对象将从本机返回。

UserDefinedObj.java is

package example.nativeobj.obj;

public class UserDefinedObj {
    public int intPrimitive;

    @Override
    public String toString() {
        StringBuffer strBuf = new StringBuffer();

        strBuf.append("UserDefinedObj.intPrimitive: ");
        strBuf.append(intPrimitive);

        return strBuf.toString();
    }
}

当 DataObject2.java 是时

package example.nativeobj.obj;

public class DataObject2 {
    public int intData;
    public String strData;
    public UserDefinedObj usrDefData;

    @Override
    public String toString() {
        StringBuffer strBuf = new StringBuffer();

        strBuf.append("\n DataObject2.intData: ");
        strBuf.append(intData);
        strBuf.append("\n DataObject2.strData: ");
        strBuf.append(strData);
        strBuf.append("\n DataObject2.usrDefData: ");
        strBuf.append(usrDefData);
//      strBuf.append("\n UserDefinedObj.intPrimitive: ");
//      strBuf.append(usrDefData.intPrimitive);

        return strBuf.toString();
    }
}

我面临崩溃问题。

但是当 DataObject.java 是时

package example.nativeobj.obj;

public class DataObject2 {
    public int intData;
    public String strData;
    public UserDefinedObj usrDefData;

    @Override
    public String toString() {
        StringBuffer strBuf = new StringBuffer();

        strBuf.append("\n DataObject2.intData: ");
        strBuf.append(intData);
        strBuf.append("\n DataObject2.strData: ");
        strBuf.append(strData);
        strBuf.append("\n DataObject2.usrDefData: ");
//      strBuf.append(usrDefData);
        strBuf.append("\n UserDefinedObj.intPrimitive: ");
        strBuf.append(usrDefData.intPrimitive);

        return strBuf.toString();
    }
}

我没有遇到崩溃问题。

区别在于下面提到的这部分代码,请观察。

//      strBuf.append(usrDefData);
        strBuf.append("\n UserDefinedObj.intPrimitive: ");
        strBuf.append(usrDefData.intPrimitive);

        strBuf.append(usrDefData);
//      strBuf.append("\n UserDefinedObj.intPrimitive: ");
//      strBuf.append(usrDefData.intPrimitive);

有人请告诉我代码中的错误吗?

这是我的本机代码:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class example_nativeobj_api_Native */

#include "custom_debug.h"

//Paths
static const char *gDataObject1ClassPath = "example/nativeobj/obj/DataObject1";
static const char *gDataObject2ClassPath = "example/nativeobj/obj/DataObject2";
static const char *gUserDefinedObjectClassPath = "example/nativeobj/obj/UserDefinedObj";

/*
 * Class:     example_nativeobj_api_Native
 * Method:    func
 * Signature: (Lexample/nativeobj/obj/DataObject1;)Lexample/nativeobj/obj/DataObject2;
 */
JNIEXPORT jobject JNICALL Java_example_nativeobj_api_Native_func
  (JNIEnv *env, jclass cls, jobject dataobj1) {

    jobject dataObject2Obj = NULL;

    /* Get a reference to obj’s class */
    jclass dataObject1Cls = (*env)->GetObjectClass(env, dataobj1);
    jclass dataObject2Cls = (*env)->FindClass(env, gDataObject2ClassPath);
    jclass userDefinedObjCls = (*env)->FindClass(env, gUserDefinedObjectClassPath);

    DBG_LOG_INFO("func: clazz references of the dataObject1Cls: %u, dataObject2Cls: %u, userDefinedObjCls: %u",
                    dataObject1Cls, dataObject2Cls, userDefinedObjCls);
    if(dataObject1Cls == NULL || dataObject2Cls == NULL || userDefinedObjCls == NULL) {
        ERROR_LOG_INFO("func: Unable to get the clazz references of the dataObject1Cls: %u, dataObject2Cls: %u, userDefinedObjCls: %u",
                dataObject1Cls, dataObject2Cls, userDefinedObjCls);
    }

    //Get the field id of the User defined object class
    jfieldID userDefObjintFid;
    userDefObjintFid = (*env)->GetFieldID(env, userDefinedObjCls, "intPrimitive", "I");
    if (userDefObjintFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the intData of the userDefinedObjCls");
        return NULL; /* failed to find the field */
    }



    //Get the data from the dataobject1 class
    ////Get the object class
    jfieldID dataObj1IntFid; /* store the field ID */
    jfieldID dataObj1StrFid;
    jfieldID dataObj1UsrDefObjFid;

    jstring jstrDataObj1 = NULL;
    const char *strDataObj1 = NULL;
    jobject userDefObj = NULL;
    jint iData1ObjInt = 0;
    jint iUsrDefObjInt = 0;

    jthrowable exc;

    /* Look for the instance field s in cls */
    dataObj1IntFid = (*env)->GetFieldID(env, dataObject1Cls, "intData", "I");
    if (dataObj1IntFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the intData of the dataObject1Cls");
        return NULL; /* failed to find the field */
    }

    dataObj1StrFid = (*env)->GetFieldID(env, dataObject1Cls, "strData", "Ljava/lang/String;");
    if (dataObj1StrFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the strData of the dataObject1Cls");
        return NULL; /* failed to find the field */
    }

    dataObj1UsrDefObjFid = (*env)->GetFieldID(env, dataObject1Cls, "usrDefData", "Lexample/nativeobj/obj/UserDefinedObj;");
    if (dataObj1UsrDefObjFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the usrDefData of the dataObject1Cls");
        return NULL; /* failed to find the field */
    }

    ////Get the values of the data members
    iData1ObjInt = (*env)->GetIntField(env, dataobj1, dataObj1IntFid);
    DBG_LOG_INFO("func: iData1ObjInt: %d", iData1ObjInt);

    jstrDataObj1 = (*env)->GetObjectField(env, dataobj1, dataObj1StrFid);
    strDataObj1 = (*env)->GetStringUTFChars(env, jstrDataObj1, NULL);
    if (strDataObj1 == NULL) {
        ERROR_LOG_INFO("func: Unable to get the string of dataObject1Cls");
        return NULL;
    }
    DBG_LOG_INFO("func: c.s = \"%s\"\n", strDataObj1);

    ////Get the user defined object here
    userDefObj = (*env)->GetObjectField(env, dataobj1, dataObj1UsrDefObjFid);
    if(userDefObj == NULL) {
        ERROR_LOG_INFO("func: Unable to get the userDefObj from dataObject1Cls");
        goto cleanup1;
    }

    //////Get the data memebers of the user define object
    iUsrDefObjInt = (*env)->GetIntField(env, userDefObj, userDefObjintFid);
    DBG_LOG_INFO("func: iUsrDefObjInt: %d", iUsrDefObjInt);

    //Creating the new dataobject2 for returning from the function
    ////Get the method id for the constructor of the class
    jmethodID constructorId = (*env)->GetMethodID(env, dataObject2Cls, "<init>", "()V");
    if(constructorId == NULL) {
        ERROR_LOG_INFO("func: Unable to get the constructor for the dataObject2Cls");
        goto cleanup1;
    }

    ////Creating the new object for the class
    dataObject2Obj = (*env)->NewObject(env, dataObject2Cls, constructorId);
    if(dataObject2Obj == NULL) {
        ERROR_LOG_INFO("func: Unable to create an object for the class dataObject2Cls");
        exc = (*env)->ExceptionOccurred(env);
        if (exc) {
            /* We don't do much with the exception, except that
            we print a debug message for it, clear it, and
            throw a new exception. */
            (*env)->ExceptionDescribe(env);

        }
        goto cleanup1;
    }


    //Create the new dataobject2
    ////Get the field id's of the objectdata2 members
    jfieldID dataObj2IntFid; /* store the field ID */
    jfieldID dataObj2StrFid;
    jfieldID dataObj2UsrDefObjFid;

    dataObj2IntFid = (*env)->GetFieldID(env, dataObject2Cls, "intData", "I");
    if (dataObj2IntFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the intData of the dataObject2Cls");
        goto cleanup1; /* failed to find the field */
    }

    dataObj2StrFid = (*env)->GetFieldID(env, dataObject2Cls, "strData", "Ljava/lang/String;");
    if (dataObj2StrFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the strData of the dataObject2Cls");
        goto cleanup1; /* failed to find the field */
    }

    dataObj2UsrDefObjFid = (*env)->GetFieldID(env, dataObject2Cls, "usrDefData", "Lexample/nativeobj/obj/UserDefinedObj;");
    if (dataObj2UsrDefObjFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the usrDefData of the dataObject2Cls");
        goto cleanup1; /* failed to find the field */
    }

    ////Set the data member values
    //////Set the int field
    (*env)->SetIntField(env, dataObject2Obj, dataObj2IntFid, (iData1ObjInt+1));
    //////Set the string field
    jstring jstr_temp = (*env)->NewStringUTF(env, strDataObj1);
    if (jstr_temp == NULL) {
        ERROR_LOG_INFO("func: Unable to create the new string utf for the dataobj2class object");
        goto cleanup1; /* out of memory */
    }
    (*env)->SetObjectField(env, dataObject2Obj, dataObj2StrFid, jstr_temp);
    //////Set teh user define object
    ////////Get the method id for the constructor of the user defined class
    jmethodID usrDefConstructorId = (*env)->GetMethodID(env, userDefinedObjCls, "<init>", "()V");
    if(usrDefConstructorId == NULL) {
        ERROR_LOG_INFO("func: Unable to get the constructor for the dataObject2Cls");
        goto cleanup1;
    }

    ////////Creating the new object for the class
    jobject userDefObject = (*env)->NewObject(env, dataObject2Cls, usrDefConstructorId);
    if(!userDefObject) {
        ERROR_LOG_INFO("func: Unable to create an object for the class userDefObject");
        goto cleanup1;
    }
    DBG_LOG_INFO("func: userDefObject: %u", userDefObject);
    DBG_LOG_INFO("func: userDefObjintFid: %u", userDefObjintFid);
    DBG_LOG_INFO("func: dataObj2UsrDefObjFid: %u", dataObj2UsrDefObjFid);

    iUsrDefObjInt += 1;
    DBG_LOG_INFO("func: iUsrDefObjInt: %d", iUsrDefObjInt);
    (*env)->SetIntField(env, userDefObject, userDefObjintFid, iUsrDefObjInt);
    ////////Set this user defined object in the data object 2 class
    (*env)->SetObjectField(env, dataObject2Obj, dataObj2UsrDefObjFid, userDefObject);

    //////////////////////////////////////////////////////////////////////////
    //For verifying if the user defined object is set
//  jobject userDefObj_Temp = (*env)->GetObjectField(env, dataObject2Obj, dataObj2UsrDefObjFid);
//  if(userDefObj == NULL) {
//      ERROR_LOG_INFO("func: Unable to get the userDefObj from dataObject1Cls");
//      goto cleanup1;
//  }
//
//  //////Get the data memebers of the user define object
//  int iUsrDefObjInt_temp = (*env)->GetIntField(env, userDefObj_Temp, userDefObjintFid);
//  DBG_LOG_INFO("func: iUsrDefObjInt_temp: %d", iUsrDefObjInt_temp);

    //////////////////////////////////////////////////////////////////////////



    cleanup1:
    //Release all the handlers
    (*env)->ReleaseStringUTFChars(env, jstrDataObj1, strDataObj1);


    //return the dataobject 2
    return dataObject2Obj;
}


JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {

    JNIEnv *env = NULL;

    DBG_LOG_INFO("JNI_OnLoad: Called in libobjnative.so.....");

    if((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_4) != JNI_OK) {
        ERROR_LOG_INFO("JNI_OnLoad: Failed to get the environment ising GetEnv");
        return -1;
    }


    return JNI_VERSION_1_4;
}


JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *jvm, void *reserved) {

    DBG_LOG_INFO("JNI_OnUnload: Called in libobjnative.so.....");

    return;
}

谢谢&问候,
S苏曼185

I am trying to return a newly created java class object in native to android. After returning the object I am facing a crash when I am trying to access the internal objects of the returned object.

As of now I am just using the NewObject() function and then setting the parameters and then returning from the native (c language) function.

Do I need to use any other function for informing the JVM and also to make that object Garbage collected?

I have uploaded whole project (Eclipse project) in the eSnips here. Click here for the android project. So that you can download and have a look at your code.

DataObject2.java class object will be returned from the native.

UserDefinedObj.java is

package example.nativeobj.obj;

public class UserDefinedObj {
    public int intPrimitive;

    @Override
    public String toString() {
        StringBuffer strBuf = new StringBuffer();

        strBuf.append("UserDefinedObj.intPrimitive: ");
        strBuf.append(intPrimitive);

        return strBuf.toString();
    }
}

When DataObject2.java is

package example.nativeobj.obj;

public class DataObject2 {
    public int intData;
    public String strData;
    public UserDefinedObj usrDefData;

    @Override
    public String toString() {
        StringBuffer strBuf = new StringBuffer();

        strBuf.append("\n DataObject2.intData: ");
        strBuf.append(intData);
        strBuf.append("\n DataObject2.strData: ");
        strBuf.append(strData);
        strBuf.append("\n DataObject2.usrDefData: ");
        strBuf.append(usrDefData);
//      strBuf.append("\n UserDefinedObj.intPrimitive: ");
//      strBuf.append(usrDefData.intPrimitive);

        return strBuf.toString();
    }
}

I am facing the crash issue.

But when DataObject.java is

package example.nativeobj.obj;

public class DataObject2 {
    public int intData;
    public String strData;
    public UserDefinedObj usrDefData;

    @Override
    public String toString() {
        StringBuffer strBuf = new StringBuffer();

        strBuf.append("\n DataObject2.intData: ");
        strBuf.append(intData);
        strBuf.append("\n DataObject2.strData: ");
        strBuf.append(strData);
        strBuf.append("\n DataObject2.usrDefData: ");
//      strBuf.append(usrDefData);
        strBuf.append("\n UserDefinedObj.intPrimitive: ");
        strBuf.append(usrDefData.intPrimitive);

        return strBuf.toString();
    }
}

I am not facing the crash issue.

Difference is in this part of the code mentioned below, please observe.

//      strBuf.append(usrDefData);
        strBuf.append("\n UserDefinedObj.intPrimitive: ");
        strBuf.append(usrDefData.intPrimitive);

and

        strBuf.append(usrDefData);
//      strBuf.append("\n UserDefinedObj.intPrimitive: ");
//      strBuf.append(usrDefData.intPrimitive);

will anyone please let me know the error in my code.

This is my native code:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class example_nativeobj_api_Native */

#include "custom_debug.h"

//Paths
static const char *gDataObject1ClassPath = "example/nativeobj/obj/DataObject1";
static const char *gDataObject2ClassPath = "example/nativeobj/obj/DataObject2";
static const char *gUserDefinedObjectClassPath = "example/nativeobj/obj/UserDefinedObj";

/*
 * Class:     example_nativeobj_api_Native
 * Method:    func
 * Signature: (Lexample/nativeobj/obj/DataObject1;)Lexample/nativeobj/obj/DataObject2;
 */
JNIEXPORT jobject JNICALL Java_example_nativeobj_api_Native_func
  (JNIEnv *env, jclass cls, jobject dataobj1) {

    jobject dataObject2Obj = NULL;

    /* Get a reference to obj’s class */
    jclass dataObject1Cls = (*env)->GetObjectClass(env, dataobj1);
    jclass dataObject2Cls = (*env)->FindClass(env, gDataObject2ClassPath);
    jclass userDefinedObjCls = (*env)->FindClass(env, gUserDefinedObjectClassPath);

    DBG_LOG_INFO("func: clazz references of the dataObject1Cls: %u, dataObject2Cls: %u, userDefinedObjCls: %u",
                    dataObject1Cls, dataObject2Cls, userDefinedObjCls);
    if(dataObject1Cls == NULL || dataObject2Cls == NULL || userDefinedObjCls == NULL) {
        ERROR_LOG_INFO("func: Unable to get the clazz references of the dataObject1Cls: %u, dataObject2Cls: %u, userDefinedObjCls: %u",
                dataObject1Cls, dataObject2Cls, userDefinedObjCls);
    }

    //Get the field id of the User defined object class
    jfieldID userDefObjintFid;
    userDefObjintFid = (*env)->GetFieldID(env, userDefinedObjCls, "intPrimitive", "I");
    if (userDefObjintFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the intData of the userDefinedObjCls");
        return NULL; /* failed to find the field */
    }



    //Get the data from the dataobject1 class
    ////Get the object class
    jfieldID dataObj1IntFid; /* store the field ID */
    jfieldID dataObj1StrFid;
    jfieldID dataObj1UsrDefObjFid;

    jstring jstrDataObj1 = NULL;
    const char *strDataObj1 = NULL;
    jobject userDefObj = NULL;
    jint iData1ObjInt = 0;
    jint iUsrDefObjInt = 0;

    jthrowable exc;

    /* Look for the instance field s in cls */
    dataObj1IntFid = (*env)->GetFieldID(env, dataObject1Cls, "intData", "I");
    if (dataObj1IntFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the intData of the dataObject1Cls");
        return NULL; /* failed to find the field */
    }

    dataObj1StrFid = (*env)->GetFieldID(env, dataObject1Cls, "strData", "Ljava/lang/String;");
    if (dataObj1StrFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the strData of the dataObject1Cls");
        return NULL; /* failed to find the field */
    }

    dataObj1UsrDefObjFid = (*env)->GetFieldID(env, dataObject1Cls, "usrDefData", "Lexample/nativeobj/obj/UserDefinedObj;");
    if (dataObj1UsrDefObjFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the usrDefData of the dataObject1Cls");
        return NULL; /* failed to find the field */
    }

    ////Get the values of the data members
    iData1ObjInt = (*env)->GetIntField(env, dataobj1, dataObj1IntFid);
    DBG_LOG_INFO("func: iData1ObjInt: %d", iData1ObjInt);

    jstrDataObj1 = (*env)->GetObjectField(env, dataobj1, dataObj1StrFid);
    strDataObj1 = (*env)->GetStringUTFChars(env, jstrDataObj1, NULL);
    if (strDataObj1 == NULL) {
        ERROR_LOG_INFO("func: Unable to get the string of dataObject1Cls");
        return NULL;
    }
    DBG_LOG_INFO("func: c.s = \"%s\"\n", strDataObj1);

    ////Get the user defined object here
    userDefObj = (*env)->GetObjectField(env, dataobj1, dataObj1UsrDefObjFid);
    if(userDefObj == NULL) {
        ERROR_LOG_INFO("func: Unable to get the userDefObj from dataObject1Cls");
        goto cleanup1;
    }

    //////Get the data memebers of the user define object
    iUsrDefObjInt = (*env)->GetIntField(env, userDefObj, userDefObjintFid);
    DBG_LOG_INFO("func: iUsrDefObjInt: %d", iUsrDefObjInt);

    //Creating the new dataobject2 for returning from the function
    ////Get the method id for the constructor of the class
    jmethodID constructorId = (*env)->GetMethodID(env, dataObject2Cls, "<init>", "()V");
    if(constructorId == NULL) {
        ERROR_LOG_INFO("func: Unable to get the constructor for the dataObject2Cls");
        goto cleanup1;
    }

    ////Creating the new object for the class
    dataObject2Obj = (*env)->NewObject(env, dataObject2Cls, constructorId);
    if(dataObject2Obj == NULL) {
        ERROR_LOG_INFO("func: Unable to create an object for the class dataObject2Cls");
        exc = (*env)->ExceptionOccurred(env);
        if (exc) {
            /* We don't do much with the exception, except that
            we print a debug message for it, clear it, and
            throw a new exception. */
            (*env)->ExceptionDescribe(env);

        }
        goto cleanup1;
    }


    //Create the new dataobject2
    ////Get the field id's of the objectdata2 members
    jfieldID dataObj2IntFid; /* store the field ID */
    jfieldID dataObj2StrFid;
    jfieldID dataObj2UsrDefObjFid;

    dataObj2IntFid = (*env)->GetFieldID(env, dataObject2Cls, "intData", "I");
    if (dataObj2IntFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the intData of the dataObject2Cls");
        goto cleanup1; /* failed to find the field */
    }

    dataObj2StrFid = (*env)->GetFieldID(env, dataObject2Cls, "strData", "Ljava/lang/String;");
    if (dataObj2StrFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the strData of the dataObject2Cls");
        goto cleanup1; /* failed to find the field */
    }

    dataObj2UsrDefObjFid = (*env)->GetFieldID(env, dataObject2Cls, "usrDefData", "Lexample/nativeobj/obj/UserDefinedObj;");
    if (dataObj2UsrDefObjFid == NULL) {
        ERROR_LOG_INFO("func: Unable to get the usrDefData of the dataObject2Cls");
        goto cleanup1; /* failed to find the field */
    }

    ////Set the data member values
    //////Set the int field
    (*env)->SetIntField(env, dataObject2Obj, dataObj2IntFid, (iData1ObjInt+1));
    //////Set the string field
    jstring jstr_temp = (*env)->NewStringUTF(env, strDataObj1);
    if (jstr_temp == NULL) {
        ERROR_LOG_INFO("func: Unable to create the new string utf for the dataobj2class object");
        goto cleanup1; /* out of memory */
    }
    (*env)->SetObjectField(env, dataObject2Obj, dataObj2StrFid, jstr_temp);
    //////Set teh user define object
    ////////Get the method id for the constructor of the user defined class
    jmethodID usrDefConstructorId = (*env)->GetMethodID(env, userDefinedObjCls, "<init>", "()V");
    if(usrDefConstructorId == NULL) {
        ERROR_LOG_INFO("func: Unable to get the constructor for the dataObject2Cls");
        goto cleanup1;
    }

    ////////Creating the new object for the class
    jobject userDefObject = (*env)->NewObject(env, dataObject2Cls, usrDefConstructorId);
    if(!userDefObject) {
        ERROR_LOG_INFO("func: Unable to create an object for the class userDefObject");
        goto cleanup1;
    }
    DBG_LOG_INFO("func: userDefObject: %u", userDefObject);
    DBG_LOG_INFO("func: userDefObjintFid: %u", userDefObjintFid);
    DBG_LOG_INFO("func: dataObj2UsrDefObjFid: %u", dataObj2UsrDefObjFid);

    iUsrDefObjInt += 1;
    DBG_LOG_INFO("func: iUsrDefObjInt: %d", iUsrDefObjInt);
    (*env)->SetIntField(env, userDefObject, userDefObjintFid, iUsrDefObjInt);
    ////////Set this user defined object in the data object 2 class
    (*env)->SetObjectField(env, dataObject2Obj, dataObj2UsrDefObjFid, userDefObject);

    //////////////////////////////////////////////////////////////////////////
    //For verifying if the user defined object is set
//  jobject userDefObj_Temp = (*env)->GetObjectField(env, dataObject2Obj, dataObj2UsrDefObjFid);
//  if(userDefObj == NULL) {
//      ERROR_LOG_INFO("func: Unable to get the userDefObj from dataObject1Cls");
//      goto cleanup1;
//  }
//
//  //////Get the data memebers of the user define object
//  int iUsrDefObjInt_temp = (*env)->GetIntField(env, userDefObj_Temp, userDefObjintFid);
//  DBG_LOG_INFO("func: iUsrDefObjInt_temp: %d", iUsrDefObjInt_temp);

    //////////////////////////////////////////////////////////////////////////



    cleanup1:
    //Release all the handlers
    (*env)->ReleaseStringUTFChars(env, jstrDataObj1, strDataObj1);


    //return the dataobject 2
    return dataObject2Obj;
}


JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {

    JNIEnv *env = NULL;

    DBG_LOG_INFO("JNI_OnLoad: Called in libobjnative.so.....");

    if((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_4) != JNI_OK) {
        ERROR_LOG_INFO("JNI_OnLoad: Failed to get the environment ising GetEnv");
        return -1;
    }


    return JNI_VERSION_1_4;
}


JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *jvm, void *reserved) {

    DBG_LOG_INFO("JNI_OnUnload: Called in libobjnative.so.....");

    return;
}

Thanks & Regards,
SSuman185

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯狂的代价 2024-12-31 00:35:27

发现问题了。

这是创建 UserDefinedObj 对象时出现的问题

在本机代码中我这样做是

jobject userDefObject = (*env)->NewObject(env, dataObject2Cls, usrDefConstructorId);

为“UserDefinedObj”提供“dataObject2Cls”的引用

修复了提供“userDefinedObjCls”

jobject userDefObject = (*env)->NewObject(env, userDefinedObjCls, usrDefConstructorId);

的问题,
S苏曼185

Found the problem.

It was the problem while creating the object of UserDefinedObj

In the native code I was doing like this

jobject userDefObject = (*env)->NewObject(env, dataObject2Cls, usrDefConstructorId);

Was supplying the reference of the "dataObject2Cls" for the "UserDefinedObj"

Got fixed on supplying the "userDefinedObjCls"

jobject userDefObject = (*env)->NewObject(env, userDefinedObjCls, usrDefConstructorId);

Regards,
SSuman185

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文