How to call an a overridden super class method in

2020-04-05 19:26发布

I'm attempting to override an activity callback method with a native implementation (to hook Lua into an activity). However I've hit a snag trying to call the superclass method within JNI code, as is required for the callback.

For example, I have a class like this:

class TestActivity extends Activity {

    @Override
    protected native void onCreate(Bundle bundle);

    static {
        System.loadLibrary("test-jni")
    }
}

And I'm trying to implement TestActivity.onCreate() in C it like this:

void Java_test_TestActivity_onCreate(JNIEnv* env, jobject obj, jobject bundle)
{
    // Get class, super class, and super class method ID...
    jclass objcls = (*env)->GetObjectClass(env, obj);
    jclass sprcls = (*env)->GetSuperclass(env, objcls);
    jmethodID methid = (*env)->GetMethodID(env, sprcls, "onCreate", "(Landroid/os/Bundle;)V");

    // Call super class method...
    (*env)->CallVoidMethod(env, obj, methid, bundle);
}

However, when I try this code, TestActivity.onCreate() is called within itself instead of Activity.onCreate(), Producing this error:

JNI ERROR (app bug): local reference table overflow (max=512)

I thought a jmethodID was specific to a class and would identify the super method, however this wasn't the case.

So my question is, how do you implement this...

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
}

In JNI?

2条回答
太酷不给撩
2楼-- · 2020-04-05 20:02

You could do this:

protected void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    onCreate0(bundle);
}

private native void onCreate0(Bundle bundle);
查看更多
登录 后发表回答