简体   繁体   English

Android中的java.lang.ExceptionInInitializerError

[英]java.lang.ExceptionInInitializerError in Android

I'm trying to create an Android Application that can verify a face But when I try to run my app on my emulator (using Eclipse), I have this result in my logcat: 我正在尝试创建一个可以验证面部的Android应用程序但是当我尝试在我的模拟器上运行我的应用程序(使用Eclipse)时,我在logcat中得到了这个结果:

       06-16 14:51:24.326: E/AndroidRuntime(4305): FATAL EXCEPTION: main
06-16 14:51:24.326: E/AndroidRuntime(4305): Process: com.neurotec.samples.faceverification, PID: 4305
06-16 14:51:24.326: E/AndroidRuntime(4305): java.lang.ExceptionInInitializerError
06-16 14:51:24.326: E/AndroidRuntime(4305):     at com.neurotec.view.NGui.<clinit>(NGui.java:56)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at com.neurotec.view.NViewBase.<clinit>(NViewBase.java:30)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at java.lang.reflect.Constructor.constructNative(Native Method)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at android.view.LayoutInflater.createView(LayoutInflater.java:594)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:341)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at android.app.Activity.setContentView(Activity.java:1975)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at com.neurotec.samples.faceverification.FaceVerificationApplication.onCreate(FaceVerificationApplication.java:46)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at android.app.Activity.performCreate(Activity.java:5370)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2422)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at android.app.ActivityThread.access$800(ActivityThread.java:151)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at android.os.Handler.dispatchMessage(Handler.java:110)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at android.os.Looper.loop(Looper.java:193)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at android.app.ActivityThread.main(ActivityThread.java:5330)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at java.lang.reflect.Method.invokeNative(Native Method)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at java.lang.reflect.Method.invoke(Method.java:515)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at dalvik.system.NativeStart.main(Native Method)
06-16 14:51:24.326: E/AndroidRuntime(4305): Caused by: java.lang.ExceptionInInitializerError
06-16 14:51:24.326: E/AndroidRuntime(4305):     at com.neurotec.media.NMedia.<clinit>(NMedia.java:49)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at com.neurotec.view.NGui.<clinit>(NGui.java:47)
06-16 14:51:24.326: E/AndroidRuntime(4305):     ... 26 more
06-16 14:51:24.326: E/AndroidRuntime(4305): Caused by: java.lang.ExceptionInInitializerError
06-16 14:51:24.326: E/AndroidRuntime(4305):     at com.neurotec.lang.NCore.<clinit>(NCore.java:140)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at com.neurotec.lang.NTypes.<clinit>(NTypes.java:171)
06-16 14:51:24.326: E/AndroidRuntime(4305):     at com.neurotec.media.NMedia.<clinit>(NMedia.java:46)
06-16 14:51:24.326: E/AndroidRuntime(4305):     ... 27 more
06-16 14:51:24.326: E/AndroidRuntime(4305): Caused by: java.lang.NoClassDefFoundError: com.sun.jna.Platform
06-16 14:51:24.326: E/AndroidRuntime(4305):     at com.neurotec.lang.NCore.<clinit>(NCore.java:123)
06-16 14:51:24.326: E/AndroidRuntime(4305):     ... 29 more

The code for FaceVerificationApplication.java is as follows : FaceVerificationApplication.java的代码如下:

public class FaceVerificationApplication extends BaseActivity implements
        EnrollmentDialogListener, UserSelectionListener {

    // ===========================================================
    // Private fields
    // ===========================================================

    private static final String EXTRA_REQUEST_CODE = "request_code";
    private static final int VERIFICATION_REQUEST_CODE = 1;
    private static final int TIMEOUT = 60000;
    private boolean mAppClosing;
    private NFaceVerificationView mFaceView;

    // ===========================================================
    // Protected methods
    // ===========================================================

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nlvdemo);
        // on application start you must set NCore context
        NCore.setContext(this);

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    showProgress(R.string.msg_initialising);

                    // get NFV for the first time
                    final NFaceVerification nfv = NFV.getInstance();

                    // load settings
                    SettingsFragment.loadSettings();

                    // button implementations
                    Button mEnrollButton = (Button) findViewById(R.id.button_enroll);
                    mEnrollButton
                            .setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    EnrollmentDialogFragment enrollDialog = new EnrollmentDialogFragment();
                                    enrollDialog.show(getFragmentManager(),
                                            "enrollment");
                                }
                            });

                    Button mCancelButton = (Button) findViewById(R.id.button_cancel);
                    mCancelButton
                            .setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    showProgress(R.string.msg_cancelling);
                                    nfv.cancel();
                                    hideProgress();
                                }
                            });

                    Button mVerifyButton = (Button) findViewById(R.id.button_verify);
                    mVerifyButton
                            .setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    Bundle bundle = new Bundle();
                                    bundle.putInt(EXTRA_REQUEST_CODE,
                                            VERIFICATION_REQUEST_CODE);
                                    UserListFragment userList = (UserListFragment) UserListFragment
                                            .newInstance(nfv.getUsers(), true,
                                                    bundle);
                                    userList.show(getFragmentManager(),
                                            "verification");
                                }
                            });

                    // set frontal camera
                    String[] names = nfv.getAvailableCameraNames();
                    for (String n : names) {
                        if (n.contains("Front")) {
                            nfv.setCamera(n);
                            break;
                        }
                    }

                    mFaceView = (NFaceVerificationView) findViewById(R.id.nFaceView);
                    nfv.addCapturePreviewListener(new NFaceVerificationCapturePreviewListener() {

                        @Override
                        public void capturePreview(
                                NFaceVerificationCapturePreviewEvent arg0) {
                            mFaceView.setEvent(arg0);
                        }
                    });

                    hideProgress();
                } catch (Exception ex) {
                    hideProgress();
                    showError(ex);
                }
            }

        }).start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.nlvdemo, menu);
        return true;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        mAppClosing = true;
    }

    @Override
    public void onEnrollmentIDProvided(final String id) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    // cancel in there are any other operations in progress
                    NFV.getInstance().cancel();
                    NFaceVerificationStatus status = NFV.getInstance().enroll(
                            id, TIMEOUT, null);
                    showInfo(String.format(
                            getString(R.string.msg_operation_status),
                            status.toString()));
                } catch (Throwable e) {
                    showError(e);
                }
            }
        }).start();
    };

    @Override
    public void onUserSelected(final NFaceVerificationUser user, Bundle bundle) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    // cancel in there are any other operations in progress
                    NFV.getInstance().cancel();
                    NFaceVerificationStatus status = NFV.getInstance().verify(
                            user.getId(), TIMEOUT);
                    showInfo(String.format(
                            getString(R.string.msg_operation_status),
                            status.toString()));
                } catch (Throwable e) {
                    showError(e);
                }
            }
        }).start();
    };

    @Override
    protected void onStop() {
        if (mAppClosing) {
            NFV.getInstance().cancel();
            NFV.dispose();
        }
        super.onStop();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_clear_db) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    // cancel if there are any other operations in progress
                    NFV.getInstance().cancel();
                    NFV.getInstance().getUsers().clear();
                }
            }).start();
            return true;
        } else if (id == R.id.action_settings) {
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

How can I solve this exception ? 我怎样才能解决这个异常? Any advice is of great help . 任何建议都有很大帮助。

Hi my problems has solved . 嗨我的问题已经解决了。 I have to add jna-4.2.2.jar into my projects . 我必须将jna-4.2.2.jar添加到我的项目中。 I have got errors about native libraries in jna.jar . 我在jna.jar中遇到了有关本机库的错误。 So in this case , in Eclipse , I have to go to Preferences > Android > Build > uncheck "Force error when external jars contain native libraries". 所以在这种情况下,在Eclipse中,我必须转到Preferences> Android> Build>取消选中“当外部jar包含本机库时强制错误”。 Then my problem is solved . 然后我的问题解决了。

As already pointed out by others you have a missing dependency to a project called Java Native Access (JNA) . 正如其他人已经指出的那样,您对一个名为Java Native Access(JNA)的项目缺少依赖性。 If you look carefully at your StackTrace you see at the end this Exception: java.lang.NoClassDefFoundError: com.sun.jna.Platform . 如果仔细查看StackTrace,最后会看到此异常: java.lang.NoClassDefFoundError: com.sun.jna.Platform A short Google search ended in the repository above. 一个简短的谷歌搜索在上面的存储库中结束。

I would try to download all related jars which have the Android prefix. 我会尝试下载所有具有Android前缀的相关jar You might also need just android-arm.jar however this would break it on devices with another processor, so add them all. 您可能还需要android-arm.jar但是这会在使用其他处理器的设备上将其破坏,因此请将它们全部添加。

Here are answers, so you know how to solve it now. 这是答案,所以你现在知道如何解决它。 But this is for others. 但这是针对其他人的。 Java have a great exception message handling where you are able to find almost everything what you need to fix it. Java有一个很好的异常消息处理,您可以在其中找到解决它所需的几乎所有内容。 This is only my opinion and advice: Everytime look at CAUSED BY: part. 这只是我的意见和建议: 每次都看看CAUSED BY:part。

When you have exception, look at last caused by message. 当你有异常时,请查看最后由消息引起的。 For example here is it: Caused by: java.lang.NoClassDefFoundError: com.sun.jna.Platform. 例如,它是:引起:java.lang.NoClassDefFoundError:com.sun.jna.Platform。 Java have a lot of error classes. Java有很多错误类。 This one is error from Object->Throwable-> Error->LinkageError . 这是来自Object-> Throwable-> Error-> LinkageError的错误 Error => Problem during runtime, No class Def Found Error is typical problem for classloader. Error =>运行时出现问题,没有类Def Found Error是类加载器的典型问题。 All what you can need to explaining error is in java docs. 解释错误所需的全部内容都在java docs中。


Btw from your quetions in comments, here is solution to add external library. 从评论中的排队顺序,这里是添加外部库的解决方案。

HOW TO ADD LIBRARY TO PROJECT IN ECLIPSE: Download jar which is decsribed by others here (jna.jar) and add it to lib folder and then you have to add it to build path (then classloader will be able to find it and it will solve your problem). 如何在ECLIPSE中添加图书馆项目:在这里下载由其他人解析的jar(jna.jar)并将其添加到lib文件夹然后你必须将它添加到构建路径(然后classloader将能够找到它并且它将会解决你的问题)。 If you are using Eclipse, you can import them directly via Eclipse to your lib folder (right click on project folder, select buildpath, libraries tab and add jar) or just copy jar directly to lib folder, then rightclick and Build Path -> Add to Buildpath. 如果您正在使用Eclipse,您可以直接通过Eclipse将它们导入到您的lib文件夹(右键单击项目文件夹,选择buildpath,libraries选项卡并添加jar)或者直接将jar复制到lib文件夹,然后右键单击并构建路径 - >添加到Buildpath。

If I'm reading this right it can't find a library for Java Native Access Platform 如果我正确读它,它找不到Java Native Access Platform的库

in Eclipse in your project properties check the libraries for JNA Platform 在项目属性的Eclipse中检查JNA平台的库

java.lang.ClassNotFoundException: com.sun.jna.Platform java.lang.ClassNotFoundException:com.sun.jna.Platform

It's been a few years since I have used eclipse. 我使用eclipse已经有几年了。

You might need to add more dependencies which can be here https://github.com/java-native-access/jna/tree/master/lib if I have the correct library. 您可能需要添加更多依赖项,如果我有正确的库,可以在https://github.com/java-native-access/jna/tree/master/lib

To do so, you right click on your project in eclipse and then go to Properties(should be bottom option). 为此,您在eclipse中右键单击项目,然后转到Properties(应该是bottom选项)。

There are some options on the left, choose "Java Build Path". 左侧有一些选项,选择“Java Build Path”。 Here is where you can add "Add Jars" or "Add External Jars". 您可以在此处添加“添加罐子”或“添加外部罐子”。 If you choose Add External Jar you can install from anywhere, if you choose Add Jars you can install within the folder project. 如果选择Add External Jar,则可以从任何地方进行安装,如果选择Add Jars,则可以在文件夹项目中安装。 I would add a jar folder inside project folder and put all jar dependencies there otherwise if you move jars to another folder you need to do this again. 我会在项目文件夹中添加一个jar文件夹并将所有jar依赖项放在那里,否则如果你将jar移动到另一个文件夹,你需要再次执行此操作。

Add all the jars from the link and sub-links that are there. 添加来自链接和子链接的所有jar。 See if that fixes the issue. 看看是否能解决问题。 Eclipse will tell you if you are missing dependencies for your classes but it won't tell you if dependencies are missing dependencies until you run your code. Eclipse将告诉您是否缺少类的依赖项,但在运行代码之前,它不会告诉您依赖项是否缺少依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM