2013-12-09 122 views
0

我正在嘗試使用jni構建一個演示應用程序(我是新手)。我已經谷歌和關於這個問題,但沒有任何幫助。未找到本地方法 - JNI Android

下面是我使用的代碼:

native.c

#include <jni.h> 

JNIEXPORT jint JNICALL com_example_MainActivity_helloint(JNIEnv* env, jobject o) 
{ 
    return (jint) 2; 
} 

MainActivity.java

public class MainActivity extends Activity { 

    static { 
     System.loadLibrary("ndk1"); 
    } 

    public native int helloint(); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     helloint(); 

    } 

} 

Android.mk

LOCAL_PATH := $(call my-dir) 
include $(CLEAR_VARS) 
LOCAL_LDLIBS := -llog 
LOCAL_MODULE := ndk1 
LOCAL_SRC_FILES := native.c 
include $(BUILD_SHARED_LIBRARY) 

該項目建立成功(包括NDK-版本),但是當我運行此,應用程序崩潰和logcat中顯示:

12-09 03:38:49.630: W/dalvikvm(2077): No implementation found for native Lcom/example/MainActivity;.helloint:()I 
12-09 03:38:49.630: D/AndroidRuntime(2077): Shutting down VM 
12-09 03:38:49.640: W/dalvikvm(2077): threadid=1: thread exiting with uncaught exception (group=0xb1ad5b90) 
12-09 03:38:49.650: E/AndroidRuntime(2077): FATAL EXCEPTION: main 
12-09 03:38:49.650: E/AndroidRuntime(2077): Process: com.example, PID: 2077 
12-09 03:38:49.650: E/AndroidRuntime(2077): java.lang.UnsatisfiedLinkError: Native method not found: com.example.MainActivity.helloint:()I 
12-09 03:38:49.650: E/AndroidRuntime(2077):  at com.example.MainActivity.helloint(Native Method) 
12-09 03:38:49.650: E/AndroidRuntime(2077):  at com.example.MainActivity.onCreate(MainActivity.java:22) 
12-09 03:38:49.650: E/AndroidRuntime(2077):  at android.app.Activity.performCreate(Activity.java:5243) 
12-09 03:38:49.650: E/AndroidRuntime(2077):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
12-09 03:38:49.650: E/AndroidRuntime(2077):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140) 
12-09 03:38:49.650: E/AndroidRuntime(2077):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226) 
12-09 03:38:49.650: E/AndroidRuntime(2077):  at android.app.ActivityThread.access$700(ActivityThread.java:135) 
12-09 03:38:49.650: E/AndroidRuntime(2077):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397) 
12-09 03:38:49.650: E/AndroidRuntime(2077):  at android.os.Handler.dispatchMessage(Handler.java:102) 
12-09 03:38:49.650: E/AndroidRuntime(2077):  at android.os.Looper.loop(Looper.java:137) 
12-09 03:38:49.650: E/AndroidRuntime(2077):  at android.app.ActivityThread.main(ActivityThread.java:4998) 
12-09 03:38:49.650: E/AndroidRuntime(2077):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-09 03:38:49.650: E/AndroidRuntime(2077):  at java.lang.reflect.Method.invoke(Method.java:515) 
12-09 03:38:49.650: E/AndroidRuntime(2077):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
12-09 03:38:49.650: E/AndroidRuntime(2077):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
12-09 03:38:49.650: E/AndroidRuntime(2077):  at dalvik.system.NativeStart.main(Native Method) 

我不知道我做錯了。任何人都可以請指點我正確的方向?

您的幫助表示感謝!

回答

2

看來我錯過了Java_函數名。

JNIEXPORT jint JNICALL Java_com_example_MainActivity_helloint(JNIEnv* env, jobject o) 
{ 
    return (jint) 2; 
} 

我不確定這是否是真正的問題,但它的確有竅門!

+1

這來自[JNI規範](http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/design.html#wp615)檢查「解析本地方法名稱」 – jcm

相關問題