2012-07-20 67 views
0

我是Android NDK的新手,所以我希望我的問題不是愚蠢的。Android NDK簡單

我跟着本教程中,這個鏈接上找到:

http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/

一切工作順利,我也跟着每一個指令。然而,當我跑的應用程序,它讓我在我的logcat這些錯誤:

07-20 07:35:25.253: D/dalvikvm(390): Trying to load lib /data/data/com.example.testndk/lib/libndkfoo.so 0x40515310 
07-20 07:35:25.253: D/dalvikvm(390): Added shared lib /data/data/com.example.testndk/lib/libndkfoo.so 0x40515310 
07-20 07:35:25.253: D/dalvikvm(390): No JNI_OnLoad found in /data/data/com.example.testndk/lib/libndkfoo.so 0x40515310, skipping init 
07-20 07:35:25.503: W/dalvikvm(390): No implementation found for native Lcom/example/testndk/NdkFooActivity;.invokeNativeFunction()Ljava/lang/String; 
07-20 07:35:25.513: D/AndroidRuntime(390): Shutting down VM 
07-20 07:35:25.513: W/dalvikvm(390): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
07-20 07:35:25.534: E/AndroidRuntime(390): FATAL EXCEPTION: main 
07-20 07:35:25.534: E/AndroidRuntime(390): java.lang.UnsatisfiedLinkError: invokeNativeFunction 
07-20 07:35:25.534: E/AndroidRuntime(390): at com.example.testndk.NdkFooActivity.invokeNativeFunction(Native Method) 
07-20 07:35:25.534: E/AndroidRuntime(390): at com.example.testndk.NdkFooActivity.onCreate(NdkFooActivity.java:23) 
07-20 07:35:25.534: E/AndroidRuntime(390): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
07-20 07:35:25.534: E/AndroidRuntime(390): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
07-20 07:35:25.534: E/AndroidRuntime(390): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
07-20 07:35:25.534: E/AndroidRuntime(390): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
07-20 07:35:25.534: E/AndroidRuntime(390): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
07-20 07:35:25.534: E/AndroidRuntime(390): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-20 07:35:25.534: E/AndroidRuntime(390): at android.os.Looper.loop(Looper.java:123) 
07-20 07:35:25.534: E/AndroidRuntime(390): at android.app.ActivityThread.main(ActivityThread.java:3683) 
07-20 07:35:25.534: E/AndroidRuntime(390): at java.lang.reflect.Method.invokeNative(Native Method) 
07-20 07:35:25.534: E/AndroidRuntime(390): at java.lang.reflect.Method.invoke(Method.java:507) 
07-20 07:35:25.534: E/AndroidRuntime(390): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
07-20 07:35:25.534: E/AndroidRuntime(390): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
07-20 07:35:25.534: E/AndroidRuntime(390): at dalvik.system.NativeStart.main(Native Method) 

沒有人有輕微的線索,該問題可能是什麼呢?我試圖弄明白,甚至從頭開始教程,但問題仍然存在。提前致謝。

我的Android的Java代碼:

package com.example.testndk; 

import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 

public class NdkFooActivity extends Activity { 

     // load the library - name matches jni/Android.mk 
     static { 
     System.loadLibrary("ndkfoo"); 
     } 

     // declare the native code function - must match ndkfoo.c 
     private native String invokeNativeFunction(); 

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

      // this is where we call the native code 
      String hello = invokeNativeFunction(); 

      new AlertDialog.Builder(this).setMessage(hello).show(); 
     } 

} 

C源代碼:

#include <string.h> 
#include <jni.h> 

jstring Java_com_mindtherobot_samples_ndkfoo_NdkFooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) { 
    return (*env)->NewStringUTF(env, "Hello from native code!"); 
} 
+0

您的c函數名稱與您的java類的完全限定名稱不匹配,請檢查我的答案。 – Nermeen 2012-07-20 07:56:56

+0

working thanks :) – progdoc 2012-07-20 08:07:32

回答

1

你有java.lang.UnsatisfiedLinkError因爲,可能是C函數名稱不匹配您的Java類的完全限定名。檢查ndkfoo.c以匹配目標Java類的包和類名:

jstring Java_com_mindtherobot_samples_ndkfoo_NdkFooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis)

這應該是:

jstring Java_com_example_testndk_NdkFooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis)

或Java項目更改包名稱:package com.mindtherobot.samples.ndkfoo;

然後,您需要在測試之前單擊[Project] - > [Clean],以便eclipse知道它必須重新編譯。