Sultan Ahmed
Sultan Ahmed

Reputation: 2220

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:

       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 :

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 .

Upvotes: 8

Views: 18660

Answers (5)

Sultan Ahmed
Sultan Ahmed

Reputation: 2220

Hi my problems has solved . I have to add jna-4.2.2.jar into my projects . I have got errors about native libraries in 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". Then my problem is solved .

Upvotes: 4

Hrabosch
Hrabosch

Reputation: 1583

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. This is only my opinion and advice: Everytime look at 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 have a lot of error classes. This one is error from Object->Throwable->Error->LinkageError. Error => Problem during runtime, No class Def Found Error is typical problem for classloader. All what you can need to explaining error is in 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). 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.

Upvotes: 5

rekire
rekire

Reputation: 47985

As already pointed out by others you have a missing dependency to a project called 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. A short Google search ended in the repository above.

I would try to download all related jars which have the Android prefix. You might also need just android-arm.jar however this would break it on devices with another processor, so add them all.

Upvotes: 1

applecrusher
applecrusher

Reputation: 5668

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.

To do so, you right click on your project in eclipse and then go to Properties(should be bottom option).

There are some options on the left, choose "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. 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.

Add all the jars from the link and sub-links that are there. 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.

Upvotes: 0

Danny Beaumont
Danny Beaumont

Reputation: 356

If I'm reading this right it can't find a library for Java Native Access Platform

in Eclipse in your project properties check the libraries for JNA Platform

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

It's been a few years since I have used eclipse.

Upvotes: 0

Related Questions