JNI:将CallVoidMethod()与用户定义的类对象一起使用

时间:2014-02-24 22:49:52

标签: java android c++ android-ndk java-native-interface

我已经在Java中定义了一个名为MyList的类,我想创建它并将其作为参数传递给C ++中的Java方法。我已经成功创建了对象,并且我调用了回调方法,但我从未看到在Java中调用回调。没有错误或例外:

jobject obj;
jclass  lClass;

// Get constructor method for MyList Class
mMyWatcher.MethodConst = mMyWatcher.mThreadEnv->GetMethodID(mMyWatcher.ClassMyList, "<init>", "()V");
if (mMyWatcher.MethodConst == NULL)
{
    LOGE("Could not find constructor method from class");
}

// Create new Java object of class MyList (call constructor basically)
obj = mMyWatcher.mThreadEnv->NewObject(mMyWatcher.ClassMyList, mMyWatcher.MethodConst);

if (mMyWatcher.MethodConst == NULL)
{
    LOGE("Could not create object from class & constructor");
}

// Get the MyList class for the new object
lClass = mMyWatcher.mThreadEnv->GetObjectClass(obj);
if (lClass == NULL)
{
    LOGE("New MyList class is NULL");
}

// Get the methods of the new class
LOGI("Getting setName()");
mMyWatcher.MethodSetName  = mMyWatcher.mThreadEnv->GetMethodID(lClass, "setName", "(Ljava/lang/String;)V");
if (mMyWatcher.MethodSetName == 0) {
    LOGE("Unable to find MyList constructor method");
}
mMyWatcher.MethodSetIndex = mMyWatcher.mThreadEnv->GetMethodID(lClass, "setIndex", "(I)V");
if (mMyWatcher.MethodSetIndex == 0) {
    LOGE("Unable to find MyList setIndex() method");
    //goto ERROR;
}
LOGI("Getting setMin()");
mMyWatcher.MethodSetMin   = mMyWatcher.mThreadEnv->GetMethodID(lClass, "setMin", "(II)V");
if (mMyWatcher.MethodSetMin == 0) {
    LOGE("Unable to find MyList constructor method");
}
LOGI("Getting setMax()");
mMyWatcher.MethodSetMax   = mMyWatcher.mThreadEnv->GetMethodID(lClass, "setMax", "(II)V");
if (mMyWatcher.MethodSetMax == 0) {
    LOGE("Unable to find MyList constructor method");
}


// Call the methods to set parameters
jstring lValue = mMyWatcher.mThreadEnv->NewStringUTF("My_PARAM_1");
mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodSetName, lValue);
mMyWatcher.mThreadEnv->DeleteLocalRef(lValue);

mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodSetIndex, 0);
mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodSetMin, 0, 0);
mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodSetMax, 0, 0);

lValue = mMyWatcher.mThreadEnv->NewStringUTF("Parm_800");
mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodAdd, 800, lValue);
mMyWatcher.mThreadEnv->DeleteLocalRef(lValue);
//This method never gets called in the java code, but there is no error
mMyWatcher.mThreadEnv->CallVoidMethod(mMyWatcher.mObject, mMyWatcher.MethodOnListUpdate2, obj);

我不知道为什么在MethodOnListUpdate2中定义的函数永远不会在Java中调用。如果我更改了我的特定类MyList,对于String,我会在Java中获得回调。我觉得它与使用自定义类有关,但我不能指出它。我之前已经缓存了mObject类和MethodOnListUpdate2函数:

mMyWatcher.MethodOnListUpdate2 = pEnv->GetMethodID(mMyWatcher.ClassMyObject, "onListUpdate2", "(Lcom/example/test/MyList;)V");
if (mMyWatcher.MethodOnListUpdate2 == NULL) {
    LOGE("JNI: Error caching the methods: onListUpdate2()");
    goto ERROR;
}

上面的代码没有问题,找到了方法。

0 个答案:

没有答案