我正在使用一个c本机函数来更改java类的整数字段。基本上,我想看看如何使用JNI实现引用调用。但是我得到的错误是"找不到本机的实现"。有人能帮我解决这个问题吗。我是android和java的新手。下面是我写的代码。如果有人能改变这一点来满足我的需求,我一定会感谢他的帮助。
java代码:
class myclass {
int a = 1;
int b = 2;
public native void callTochangeJNI();
static {
System.loadLibrary("sum_jni");
}
}
public class sum extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sum);
myclass myobject = new myclass();
System.out.println("calling the jni native method");
myobject.callTochangeJNI();
System.out.println("returned from jni native method one");
myobject.callTochangeJNI();
System.out.format(" a = %d%n....", myobject.a);
System.out.format("b = %d%n...", myobject.b);
}
}
c本机方法:
void Java_com_kpit_myfirstndkapp_sum_callTochangeJNI(JNIEnv* env, jobject obj )
{
jfieldID fid;
jmethodID mid;
jclass myclass;
printf(" Inside the JNI native method n");
jclass localRefCls = (*env)->FindClass(env, "java/lang/String");
myclass = (*env)->NewGlobalRef(env, localRefCls);
fid = (*env)->GetFieldID(env,myclass," a ","I");
(*env)->SetIntField(env, obj ,fid,3);
fid = (*env)->GetFieldID(env,myclass," b ","I");
(*env)->SetIntField(env, obj ,fid,4);
}
下面是我得到的日志:
07-25 09:33:07.723: D/dalvikvm(573): Trying to load lib /data/da/com.kpit.myfirstndkapp /lib/libsum_jni.so 0x4129d7b8
07-25 09:33:07.723: D/dalvikvm(573): Added shared lib /data/data/com.kpit.myfirstndkapp/lib/libsum_jni.so 0x4129d7b8
07-25 09:33:07.723: D/dalvikvm(573): No JNI_OnLoad found in /data/data/com.kpit.myfirstndkapp/lib/libsum_jni.so 0x4129d7b8, skipping init
07-25 09:33:07.723: I/System.out(573): calling the jni native method
07-25 09:33:07.723: W/dalvikvm(573): No implementation found for native Lcom/kpit/myfirstndkapp/myclass;.callTochangeJNI ()V
07-25 09:33:07.733: I/dalvikvm(573): Wrote stack traces to '/data/anr/traces.txt'
07-25 09:33:07.804: D/AndroidRuntime(573): Shutting down VM
07-25 09:33:07.804: W/dalvikvm(573): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
07-25 09:33:07.854: E/AndroidRuntime(573): FATAL EXCEPTION: main
07-25 09:33:07.854: E/AndroidRuntime(573): java.lang.UnsatisfiedLinkError: callTochangeJNI
07-25 09:33:07.854: E/AndroidRuntime(573): at com.kpit.myfirstndkapp.myclass.callTochangeJNI(Native Method)
07-25 09:33:07.854: E/AndroidRuntime(573): at com.kpit.myfirstndkapp.sum.onCreate(sum.java:27)
07-25 09:33:07.854: E/AndroidRuntime(573): at android.app.Activity.performCreate(Activity.java:4465)
07-25 09:33:07.854: E/AndroidRuntime(573): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-25 09:33:07.854: E/AndroidRuntime(573): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-25 09:33:07.854: E/AndroidRuntime(573): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-25 09:33:07.854: E/AndroidRuntime(573): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-25 09:33:07.854: E/AndroidRuntime(573): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-25 09:33:07.854: E/AndroidRuntime(573): at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 09:33:07.854: E/AndroidRuntime(573): at android.os.Looper.loop(Looper.java:137)
07-25 09:33:07.854: E/AndroidRuntime(573): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-25 09:33:07.854: E/AndroidRuntime(573): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 09:33:07.854: E/AndroidRuntime(573): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 09:33:07.854: E/AndroidRuntime(573): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-25 09:33:07.854: E/AndroidRuntime(573): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-25 09:33:07.854: E/AndroidRuntime(573): at dalvik.system.NativeStart.main(Native Method)
07-25 09:33:08.234: I/dalvikvm(573): threadid=3: reacting to signal 3
07-25 09:33:08.264: I/dalvikvm(573): Wrote stack traces to '/data/anr/traces.txt'
07-25 09:33:08.554: I/dalvikvm(573): threadid=3: reacting to signal 3
07-25 09:33:08.623: I/dalvikvm(573): Wrote stack traces to '/data/anr/traces.txt'
07-25 09:38:07.994: I/Process(573): Sending signal. PID: 573 SIG: 9
请帮助我
您的C方法的名称和签名错误,名称必须是Java_PackageName_ClassName_CfunctionName
如果您从名为MyClass
的类中调用它,并且它的包名称为com.kpit.myfirstndkapp
,那么您的方法必须声明为
JNIEXPORT void JNICALL Java_com_kpit_myfirstndkapp_MyClass_callToChangeJNI(...)
最后,经过一些调试,我得到了将a和b的值更改为新值的确切代码。这是正确的代码:
Java code:
package com.kpit.myfirstndkapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
class myclass {
int a = 1;
int b = 2;
public native void callTochangeJNI();
static {
System.loadLibrary("sum_jni");
}
}
public class sum extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sum);
myclass myobject = new myclass();
System.out.println("calling the jni native method");
myobject.callTochangeJNI();
System.out.println("returned from jni native method one");
System.out.format(" a = %d%n....", myobject.a);
System.out.format("b = %d%n...", myobject.b);
}}
C native library code:
#include <stdio.h>
#include <stdlib.h>
#include <jni.h>
void Java_com_kpit_myfirstndkapp_myclass_callTochangeJNI(JNIEnv* env,
jobject obj)
{
jfieldID fid;
jmethodID mid;
jclass myclass;
printf(" Inside the JNI native method n");
jclass cls = (*env)->GetObjectClass(env, obj);
fid = (*env)->GetFieldID(env,cls,"a","I");
(*env)->SetIntField(env, obj ,fid,3);
fid = (*env)->GetFieldID(env,cls,"b","I");
(*env)->SetIntField(env, obj ,fid,4);
}
This works and i get the below log on the screen:
07-26 06:42:30.838: I/System.out(551): calling the jni native method
07-26 06:42:30.859: I/System.out(551): returned from jni native method one
07-26 06:42:30.859: I/System.out(551): a = 3
07-26 06:42:30.868: I/System.out(551): ....b = 4
07-26 06:42:31.179: I/Process(93): Sending signal. PID: 551 SIG: 3
07-26 06:42:31.179: I/dalvikvm(551): threadid=3: reacting to signal 3
07-26 06:42:31.269: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
07-26 06:42:31.348: D/gralloc_goldfish(551): Emulator without GPU emulation detected.
07-26 06:42:31.498: I/ActivityManager(93): Displayed com.kpit.myfirstndkapp/.sum:
+2s524ms(总计+1m25s486ms)07-26 06:42:31.558:I/ActivityManager(93):显示com.android.launcher/com.android.loncher.launcher:+1m25s544ms
我非常感谢stackoverflow.com为像我这样的学习者提供这样一个帮助平台