我一直在使用HtmlUnit(版本2.9)来构建一个从网站获取数据的android应用程序。代码编译得很好,但是当我点击“Login”按钮时,它会崩溃,因为NoClassDefFoundError在到达WebUnit变量初始化时被抛出。我已将罐子添加到Project/lib
。HtmlUnit NoClassDefFoundError
相关代码:
public void doLogin(View v) {
if (uname.getText().toString() == "" || pass.getText().toString() == "") {
makeToast("Missing at least one of the following: Username, Password");
Log.e("RobloxApp", "Missing a username and/or password");
return;
} else {
try {
WebClient client = new WebClient();
HtmlPage page = client.getPage(RbxRes.Urls.loginPage);
HtmlInput unameInput = (HtmlInput) page.getElementById(RbxRes.Elements.uNameId);
HtmlInput passInput = (HtmlInput) page.getElementById(RbxRes.Elements.passId);
HtmlButton loginBtn = (HtmlButton) page.getElementById(RbxRes.Elements.loginId);
unameInput.setTextContent(uname.getText().toString());
passInput.setTextContent(pass.getText().toString());
loginBtn.click();
} catch (Exception e) {
Log.e("RobloxApp", "The following error occurred while trying to login: " + e.getMessage());
makeToast("Error: Could not login");
return;
}
}
logcat的输出:
07-14 04:38:46.357: E/AndroidRuntime(708): FATAL EXCEPTION: main
07-14 04:38:46.357: E/AndroidRuntime(708): java.lang.IllegalStateException: Could not execute method of the activity
07-14 04:38:46.357: E/AndroidRuntime(708): at android.view.View$1.onClick(View.java:3044)
07-14 04:38:46.357: E/AndroidRuntime(708): at android.view.View.performClick(View.java:3511)
07-14 04:38:46.357: E/AndroidRuntime(708): at android.view.View$PerformClick.run(View.java:14105)
07-14 04:38:46.357: E/AndroidRuntime(708): at android.os.Handler.handleCallback(Handler.java:605)
07-14 04:38:46.357: E/AndroidRuntime(708): at android.os.Handler.dispatchMessage(Handler.java:92)
07-14 04:38:46.357: E/AndroidRuntime(708): at andr oid.os.Looper.loop(Looper.java:137)
07-14 04:38:46.357: E/AndroidRuntime(708): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-14 04:38:46.357: E/AndroidRuntime(708): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 04:38:46.357: E/AndroidRuntime(708): at java.lang.reflect.Method.invoke(Method.java:511)
07-14 04:38:46.357: E/AndroidRuntime(708): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-14 04:38:46.357: E/AndroidRuntime(708): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-14 04:38:46.357: E/AndroidRuntime(708): at dalvik.system.NativeStart.main(Native Method)
07-14 04:38:46.357: E/AndroidRuntime(708): Caused by: java.lang.reflect.InvocationTargetException
07-14 04:38:46.357: E/AndroidRuntime(708): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 04:38:46.357: E/AndroidRuntime(708): at java.lang.reflect.Method.invoke(Method.java:511)
07-14 04:38:46.357: E/AndroidRuntime(708): at android.view.View$1.onClick(View.java:3039)
07-14 04:38:46.357: E/AndroidRuntime(708): ... 11 more
07-14 04:38:46.357: E/AndroidRuntime(708): Caused by: java.lang.NoClassDefFoundError: com.gargoylesoftware.htmlunit.WebClient
07-14 04:38:46.357: E/AndroidRuntime(708): at com rbx.robloxapp.Login.doLogin(Login.java:46)
07-14 04:38:46.357: E/AndroidRuntime(708): ... 14 more
它似乎的HtmlUnit的罐子是从你的classpath失踪。 – 2012-07-14 05:38:26
@Ajmal我以为我已经这样做了:http://imgur.com/hy1Pq。由于我没有足够的代表,因此我无法在线索中发布图片。 – philipfoster 2012-07-14 06:22:35
它看起来像你添加为一个外部的jar。它应该添加像,首先在你的项目中创建一个lib文件夹,并将jar文件复制到它。然后去属性使用添加jar选项并选择lib/some.jar。 – 2012-07-14 07:27:53