0

我正在使用 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
4

1 回答 1

1

真正的(嵌套的)异常是Caused by: java.lang.UnsatisfiedLinkError: Couldn't load hello-jni: findLibrary returned null

这意味着 Android 无法找到您的库“hello-jni”。确保名称是正确的,并且你已经编译它没有错误。

检查您的lib文件夹以查看您的库是否在此处 ( libhello-jni.so)。

于 2012-08-30T09:46:35.523 回答