我想在Android中運行簡單的jni代碼,但所有我越來越Unsatisfiedlinkerror。Android中的Unsatisfiedlinkerror(日食)
這裏是我的Java代碼:
package com.lipcap;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView a;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
a=new TextView(this);
String b;
MainActivity ob=new MainActivity();
b=ob.sniff();
a.setText(b);
setContentView(a);
}
public native String sniff();
static{
System.loadLibrary("native");
}
}
這裏是我的C++代碼(在$ PROJECT_PATH/JNI /):
#include<iostream>
#include<string.h>
#include<jni.h>
JNIEXPORT jstring JNICALL Java_com_lipcap_MainActivity_sniff
(JNIEnv *env, jobject obj){
return env->NewStringUTF("This is Native");
}
我已經使用javac遵守Java代碼,並提出了頭文件使用javah。
然後我運行了ndk-build。 然後我從eclipse中運行代碼(安裝了apk的android)。
我得到這個錯誤:
E/AndroidRuntime( 769): FATAL EXCEPTION: main
E/AndroidRuntime( 769): java.lang.UnsatisfiedLinkError: sniff
E/AndroidRuntime( 769): at com.lipcap.MainActivity.sniff(Native Method)
E/AndroidRuntime( 769): at com.lipcap.MainActivity.onCreate(MainActivity.java:36)
E/AndroidRuntime( 769): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 769): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime( 769): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime( 769): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime( 769): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime( 769): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 769): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 769): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 769): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 769): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 769): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 769): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 769): at dalvik.system.NativeStart.main(Native Method)
我沒有設置LD_LIBRARY_PATH。
但是,沒有設置LD_LIBRARY_PATH樣例代碼,例如NDK提供的HelloJNI,運行非常好。
請告訴我我在哪裏失蹤。
'MainActivity OB =新MainActivity();'爲什麼?您已經進入了MainActivity的實例。必須使用'this.sniff()'。 – 2011-06-14 21:33:33
是的,可以使用this.sniff。 無論如何,就Unsatisfiedlinkerror而言,這不會產生任何差異。 – d34th4ck3r 2011-06-14 21:41:27
從C++更改代碼到C一切工作正常.. :) – d34th4ck3r 2011-06-15 20:14:01