我一直在使用 HtmlUnit(2.9 版)构建一个从网站获取数据的 android 应用程序。代码编译得很好,但是当我单击“登录”按钮时,它会崩溃,因为在到达 WebUnit 变量初始化时会抛出 NoClassDefFoundError。我已将罐子添加到Project/lib
.
相关代码:
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;
}
}
日志输出:
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