问题描述
举例来说,我们说,在Android中,我需要调用静态方法 android.os.SystemClock.elapsedRealtime()
,它返回一个长期的,从部分本土code。在 mylib.c
文件,我有
For example, let say that in Android, I need to call the static method android.os.SystemClock.elapsedRealtime()
, which returns a long, from a portion of native code. In the mylib.c
file, I have
JNIEXPORT jlong JNICALL
Java_com_mpackage_MyClass_nativeMethod(JNIEnv *env, jobject obj){
jclass cls = (*env)->GetObjectClass(env, obj);
jmethodID mid = (*env)->GetStaticMethodID(env, cls,
"android.os.SystemClock.elapsedRealtime", "(V)J");
if (mid == 0)
return 0L;
return CallStaticLongMethod(cls, mid);
}
在Java的 MyClass.class
,我在其他
In the java MyClass.class
, I have among others
static {System.loadLibrary("myLib");}
native long nativeMethod();
但是当我把它,我得到
but when I call it, I get
ERROR/AndroidRuntime(628): java.lang.NoSuchMethodError:
android.os.SystemClock.elapsedRealtime()
在中期
行的声明。我认为这是简单的,但我是新来JNI。有人能指出我的错误(县)?
at the declaration of mid
line. I think this is straightforward but I'm new to jni. Can someone point out my mistake(s) ?
推荐答案
看起来像你的JNI API的使用是不妥当的。首先,你应该得到 android.os.SystemClock
的类引用。作为参数传递的OBJ,是 MyClass的
的对象。您应该使用(* ENV) - >的findClass(ENV,机器人/ OS / SystemClock)
来获得JCLASS为SystemClock。然后调用(* ENV) - > GetStaticMethodID(ENV,CLS,elapsedRealtime,(五)J);
获得方法ID。看看在 JNI教程的进一步的细节。
Looks like your usage of the JNI API is not proper.First you should get the class reference of android.os.SystemClock
. The obj passed as a parameter, is an object of MyClass
. You should use (*env)->FindClass(env, "android/os/SystemClock")
to get a jclass for the SystemClock. Then call (*env)->GetStaticMethodID(env, cls,"elapsedRealtime", "(V)J");
to get the method id. Take a look at the JNI tutorial for further details
这篇关于从本地code调用另一个包的静态Java方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!