2

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

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

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

公共原生对象 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

请帮我 。

谢谢

4

1 回答 1

9

我犯了一个小错误,我通过对 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;
}
于 2012-12-14T12:53:20.400 回答