如何在JNI中实例化一个类

时间:2012-12-14 11:12:32

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

我很惊讶这个非常重要的细节缺少在互联网上的全面演练。或者也许我是一个最糟糕的Google员工。或者是一个完全愚蠢的程序员。

我几个月来一直在研究android,但最近我被告知要做JNI。我的C技能已经生锈了。然而,当我坚持这一点并且无法继续前进时,我正赶上来。

我想要一些简单的东西。这是我在MainActivity中的原生方法声明

public native Object createObject();

这就是我打算如何使用它

    Object abc = createObject();
    Integer num = (Integer) abc;

我只想从C创建一个java.lang.Integer对象,并将其发送给java。它不能得到任何更简单的

    jobject Java_com_example_hellojni_HelloJni_callbackJava(JNIEnv * env, jobject this )
{
    jclass cls = (*env)->FindClass(env, "java/lang/Integer");
    jmethodID methodID = (*env)->GetMethodID(env, cls, "<init>", "(I)V");
    jint number = 8 ; 
    return (*env)->NewObject(cls, methodID, number);

}

(android ndk r8c) 我得到了堆栈跟踪的问候,这会导致我的应用程序崩溃。

12-14 16:10:07.595: D/dalvikvm(302): Trying to load lib /data/data/com.example.hellojni/lib/libhello-jni.so 0x45f3da60
12-14 16:10:07.625: D/dalvikvm(302): Added shared lib /data/data/com.example.hellojni/lib/libhello-jni.so 0x45f3da60
12-14 16:10:07.625: D/dalvikvm(302): No JNI_OnLoad found in /data/data/com.example.hellojni/lib/libhello-jni.so 0x45f3da60, skipping init

12-14 16:10:07.684: W/dalvikvm(302): **JNI WARNING: threadid=1 using env from threadid=0**
12-14 16:10:07.684: W/dalvikvm(302):              in Lcom/example/hellojni/HelloJni;.callbackJava ()V (NewObject)
12-14 16:10:07.684: I/dalvikvm(302): "main" prio=5 tid=1 NATIVE
12-14 16:10:07.684: I/dalvikvm(302):   | group="main" sCount=0 dsCount=0 s=N obj=0x4001d8e0 self=0xccb0
12-14 16:10:07.684: I/dalvikvm(302):   | sysTid=302 nice=0 sched=0/0 cgrp=default handle=-1345026008
12-14 16:10:07.684: I/dalvikvm(302):   | schedstat=( 152515888 582639885 48 )
12-14 16:10:07.684: I/dalvikvm(302):   at com.example.hellojni.HelloJni.callbackJava(Native Method)
12-14 16:10:07.695: I/dalvikvm(302):   at com.example.hellojni.HelloJni.onCreate(HelloJni.java:59)
12-14 16:10:07.695: I/dalvikvm(302):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-14 16:10:07.695: I/dalvikvm(302):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-14 16:10:07.695: I/dalvikvm(302):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-14 16:10:07.695: I/dalvikvm(302):   at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-14 16:10:07.695: I/dalvikvm(302):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-14 16:10:07.695: I/dalvikvm(302):   at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 16:10:07.695: I/dalvikvm(302):   at android.os.Looper.loop(Looper.java:123)
12-14 16:10:07.695: I/dalvikvm(302):   at android.app.ActivityThread.main(ActivityThread.java:4627)
12-14 16:10:07.695: I/dalvikvm(302):   at java.lang.reflect.Method.invokeNative(Native Method)
12-14 16:10:07.695: I/dalvikvm(302):   at java.lang.reflect.Method.invoke(Method.java:521)
12-14 16:10:07.695: I/dalvikvm(302):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-14 16:10:07.695: I/dalvikvm(302):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-14 16:10:07.695: I/dalvikvm(302):   at dalvik.system.NativeStart.main(Native Method)
12-14 16:10:07.704: E/dalvikvm(302): VM aborting

请帮帮我。

由于

1 个答案:

答案 0 :(得分:8)

我做了一个微不足道的错误,我通过对JNI的更多搜索和研究发现了

我错过了将env放入NewObject的参数中。如果有人需要,这是正确的代码

jobject Java_com_example_hellojni_HelloJni_createObject(JNIEnv * env, jobject this )
{

    jclass cls = (*env)->FindClass(env, "java/lang/Integer");
    jmethodID methodID = (*env)->GetMethodID(env, cls, "<init>", "(I)V");
    jobject a=(*env)->NewObject(env,cls, methodID, 5);
    return a;
}