我正在尝试创建一个可以验证面部的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
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);
}
}
如何解决此异常?任何建议都有很大帮助。
答案 0 :(得分:3)
您好我的问题已经解决了。我必须将jna-4.2.2.jar添加到我的项目中。我在jna.jar中遇到了有关本机库的错误。所以在这种情况下,在Eclipse中,我必须转到首选项&gt; Android&gt;构建&gt;取消选中“当外部jar包含本机库时强制错误”。然后我的问题就解决了。
答案 1 :(得分:1)
正如其他人已经指出的那样,您对名为Java Native Access (JNA)的项目缺少依赖性。如果仔细查看StackTrace,最后会看到此异常: java.lang.NoClassDefFoundError: com.sun.jna.Platform
。一个简短的谷歌搜索在上面的存储库中结束。
我会尝试使用具有Android前缀的download all related jars。您可能还需要android-arm.jar
,但这会在使用其他处理器的设备上将其破坏,因此请将它们全部添加。
答案 2 :(得分:1)
这是答案,所以你现在知道如何解决它。但这是针对其他人的。 Java有一个很好的异常消息处理,您可以在其中找到解决它所需的几乎所有内容。这只是我的意见和建议: 每次都看看CAUSED BY:part。
当你有异常时,请查看最后由消息引起的。例如,它是:引起:java.lang.NoClassDefFoundError:com.sun.jna.Platform。 Java有很多错误类。这是来自Object-&gt; Throwable-&gt; Error-&gt; LinkageError 的错误。错误=&gt;运行时出现问题,没有类Def Found Error是类加载器的典型问题。解释错误所需的全部内容都在java docs中。
从评论中的排队顺序来看,这里是添加外部库的解决方案。
如何在ECLIPSE中添加图书馆 下载其他人解析的jar(jna.jar)并将其添加到 lib 文件夹然后你必须将它添加到构建路径(然后classloader将能够找到它并且它将解决你的问题问题)。如果你正在使用Eclipse,你可以直接通过Eclipse将它们导入你的lib文件夹(右键单击项目文件夹,选择buildpath,libraries选项卡并添加jar)或者只是将jar直接复制到lib文件夹,然后右键单击和Build Path - &gt;添加到Buildpath。
答案 3 :(得分:0)
如果我正确读取它,则无法找到Java Native Access Platform的库
在项目属性的Eclipse中检查JNA平台的库
java.lang.ClassNotFoundException:com.sun.jna.Platform
我使用eclipse已经有几年了。
答案 4 :(得分:0)
如果我有正确的库,您可能需要添加更多依赖项https://github.com/java-native-access/jna/tree/master/lib。
为此,您在eclipse中右键单击项目,然后转到Properties(应该是bottom选项)。
左边有一些选项,选择&#34; Java Build Path&#34;。您可以在此处添加&#34;添加Jars&#34;或者&#34;添加外部罐子&#34;。如果选择Add External Jar,则可以从任何地方进行安装,如果选择Add Jars,则可以在文件夹项目中安装。我会在项目文件夹中添加一个jar文件夹并将所有jar依赖项放在那里,否则如果你将jar移动到另一个文件夹,你需要再次执行此操作。
添加来自链接和子链接的所有jar。看看是否能解决问题。 Eclipse将告诉您是否缺少类的依赖项,但在运行代码之前,它不会告诉您依赖项是否缺少依赖项。