2012-10-20 43 views
2

我發現了一些類似的問題,但他們的回答並沒有幫助我。我閱讀了本教程http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/,並發現了一些問題。UnsatisfiedLinkError實現簡單的NDK設置

我Android.mk文件:

 LOCAL_PATH := $(call my-dir) 
MY_PATH := $(LOCAL_PATH) 
include $(call all-subdir-makefiles) 

include $(CLEAR_VARS) 

LOCAL_PATH := $(MY_PATH) 

LOCAL_LDLIBS := -llog -ldl 
LOCAL_MODULE := ndkfoo 
LOCAL_SRC_FILES := ndkfoo.c 

include $(BUILD_SHARED_LIBRARY) 

我的C級ndkfoo.c:

#include <jni.h> 
#include <string.h> 
#include <android/log.h> 
#include <string.h> 
#include <jni.h> 
jstring Java_com_example_ocrrecognise_ndkfoo_MainActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) { 
    return (*env)->NewStringUTF(env, "Hello from native code!"); 
} 

和MainActivity:

public class MainActivity extends Activity { 
static { 
    System.loadLibrary("ndkfoo"); 
} 
private native String invokeNativeFunction(); 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    final Button button = (Button) findViewById(R.id.button_id); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) 
     { 
       try{ 
        String hello = invokeNativeFunction(); 
        Log.i("string func", hello); 
       }catch (UnsatisfiedLinkError e) 
       { 
        e.printStackTrace(); 
       } 

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

} 

但我有

No implementation found for native Lcom/example/ocrrecognise/MainActivity;.invokeNativeFunction()Ljava/lang/String; 
java.lang.UnsatisfiedLinkError: invokeNativeFunction 
    at com.example.ocrrecognise.MainActivity.invokeNativeFunction(Native Method) 
    at com.example.ocrrecognise.MainActivity.access$0(MainActivity.java:14) 
    at com.example.ocrrecognise.MainActivity$1.onClick(MainActivity.java:24) 
    at android.view.View.performClick(View.java:2485) 
    at android.view.View$PerformClick.run(View.java:9080) 
    at android.os.Handler.handleCallback(Handler.java:587) 
    at android.os.Handler.dispatchMessage(Handler.java:92) 
    at android.os.Looper.loop(Looper.java:123) 
    at android.app.ActivityThread.main(ActivityThread.java:3687) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:507) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 

我在ndkfoo.c我把正確的包名稱。請幫忙。

回答

2

你的Activity在哪個包中? 請注意,應該在com.example.ocrrecognise.ndkfoo. 你是如何編譯你的c代碼的?

您是否已將.so文件編譯爲/libs/armeabi在您的項目中?

+0

非常感謝!我的簡單錯誤是,我的活動是在com.example.ocrrecognise而不是com.example.ocrrecognise.ndkfoo。修復它解決了我的問題。謝謝! –

1

從錯誤

No implementation found for native Lcom/example/ocrrecognise/MainActivity;.invokeNativeFunction 

,看來您的主要活動是在包com.example.ocrrecognise。如果你把它放在包com_example_ocrrecognise_ndkfoo中,它應該可以工作,因爲這是你在本地代碼中聲明函數時使用的前綴。