Sky
Sky

Reputation: 121

Cannot use CallVoidMethod from another object

There are two class A, B
m_Class, m_MethodID are not NULL..
But system will crash after call env->CallVoidMethod, Why?

public Class A extends Activity {
    protected void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);

        B BClass = new B();
        BClass.setFunction(this, "testFunc");
    }
    public void testFunc() {
        Log.e("", "Test");
    }
}

public Class B {

    public native void setFunction(Object caller, String method);
}

-------------JNI-------------

JNIEXPORT void JNICALL Java_com_B_setFunction(JNIEnv *env, jobject thiz, jobject clsCaller, jstring sMethod) {

    string sMethodName = jstring2str(env, sMethod);
    jclass m_Class = env->GetObjectClass(clsCaller);
    jmethodID m_MethodID = env->GetMethodID(m_Class, sMethodName.c_str(), "()V");

    env->CallVoidMethod(m_Class, m_MethodID);
}

Upvotes: 1

Views: 758

Answers (1)

Sky
Sky

Reputation: 121

I had resolve this issue.. I refer to this page Android NDK: calling java method from JNI C class and modify the code in JNI

from

env->CallVoidMethod(m_Class, m_MethodID);

to

env->CallVoidMethod(clsCaller, m_MethodID);

Because CallVoidMethod is trigger by instance(clsCaller) not class(m_Class)

Upvotes: 2

Related Questions