1

我一直在使用 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
4

2 回答 2

0

您是否在 AndroidManifest.xml 中添加了您的库,例如google map library <uses-library android:name="com.google.android.maps"/>

于 2012-07-14T05:42:57.537 回答
0

把你的罐子放进去,libs然后右键单击它并选择Add to Build Path

于 2012-07-14T06:48:06.623 回答