我正在使用jni从我的android代码调用c ++函数,因为我正在使用NDK,那时我得到例外java.lang.exceptionininitializererror。
public class HelloJni extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText( stringFromJNI() );
setContentView(tv);
}
public native String stringFromJNI();
public native String unimplementedStringFromJNI();
static {
System.loadLibrary("hello-jni");
}
}
LogCat是:
08-30 14:59:48.210: E/AndroidRuntime(10494): FATAL EXCEPTION: main
08-30 14:59:48.210: E/AndroidRuntime(10494): java.lang.ExceptionInInitializerError
08-30 14:59:48.210: E/AndroidRuntime(10494): at java.lang.Class.newInstanceImpl(Native Method)
08-30 14:59:48.210: E/AndroidRuntime(10494): at java.lang.Class.newInstance(Class.java:1409)
08-30 14:59:48.210: E/AndroidRuntime(10494): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-30 14:59:48.210: E/AndroidRuntime(10494): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
08-30 14:59:48.210: E/AndroidRuntime(10494): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-30 14:59:48.210: E/AndroidRuntime(10494): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-30 14:59:48.210: E/AndroidRuntime(10494): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-30 14:59:48.210: E/AndroidRuntime(10494): at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 14:59:48.210: E/AndroidRuntime(10494): at android.os.Looper.loop(Looper.java:130)
08-30 14:59:48.210: E/AndroidRuntime(10494): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-30 14:59:48.210: E/AndroidRuntime(10494): at java.lang.reflect.Method.invokeNative(Native Method)
08-30 14:59:48.210: E/AndroidRuntime(10494): at java.lang.reflect.Method.invoke(Method.java:507)
08-30 14:59:48.210: E/AndroidRuntime(10494): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-30 14:59:48.210: E/AndroidRuntime(10494): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-30 14:59:48.210: E/AndroidRuntime(10494): at dalvik.system.NativeStart.main(Native Method)
08-30 14:59:48.210: E/AndroidRuntime(10494): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load hello-jni: findLibrary returned null
08-30 14:59:48.210: E/AndroidRuntime(10494): at java.lang.Runtime.loadLibrary(Runtime.java:429)
08-30 14:59:48.210: E/AndroidRuntime(10494): at java.lang.System.loadLibrary(System.java:554)
08-30 14:59:48.210: E/AndroidRuntime(10494): at com.example.hellojni.HelloJni.<clinit>(HelloJni.java:64)
08-30 14:59:48.210: E/AndroidRuntime(10494): ... 15 more
答案 0 :(得分:1)
真实(嵌套)异常是Caused by: java.lang.UnsatisfiedLinkError: Couldn't load hello-jni: findLibrary returned null
这意味着Android无法找到您的库“hello-jni”。确保名称正确,并且您已编译它没有错误。
检查您的lib
文件夹,查看您的图书馆是否在此处(libhello-jni.so
)。