如何将调用的Java通过JNI一个覆盖超类的方法? [英] How to call an a overridden super class method in Java through JNI?

查看:208
本文介绍了如何将调用的Java通过JNI一个覆盖超类的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图来覆盖本机实现(挂钩的Lua成一个活动)活动回调方法。不过,我已经打了一个障碍试图调用JNI code内的超类方法,因为需要回调。

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

和我试图实施 TestActivity.onCreate()用C这样的:

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

然而,当我尝试这个code, TestActivity.onCreate()被称为内本身,而不是 Activity.onCreate(),产生这个错误:

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

JNI错误(错误的应用程序):本地参照表溢出(最大值= 512)

我以为jmethodID是具体到一个类和将确定超级方法,但是这种情况并非如此。

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

在JNI?

推荐答案

您可以尝试CallNonVirtualVoidMethod():见的http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/functions.html#wp4581

You can try CallNonVirtualVoidMethod(): see http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/functions.html#wp4581

这篇关于如何将调用的Java通过JNI一个覆盖超类的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆