[英]Android java.lang.NoClassDefFoundError: org.jsoup.Jsoup
I work with eclipse Version: Indigo Service Release 2 Build id: 20120216-1857. 我使用eclipse版本:Indigo Service Release 2 Build id:20120216-1857。 The Android version ist 2.2. Android版本是2.2。 I make an app to test a connect and parse a web site like this: 我创建了一个应用来测试连接并解析一个这样的网站:
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
Document doc = Jsoup.connect("http://example.com/").get();
Elements divs = doc.select("div#test");
for (Element div : divs) {
System.out.println(div.text());
}
} catch (Exception e) {
}
}
}
Manifest file: 清单文件:
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".TestActivity"
android:label="@string/app_name"
android:configChanges="orientation"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<uses-library android :name="org.jsoup.Jsoup" />
</activity>
</application>
I add the library jsoup-1.6.1.jar to JAVA Buildpath, but I become the runtime error: E/AndroidRuntime(736): java.lang.NoClassDefFoundError: org.jsoup.Jsoup 我将库jsoup-1.6.1.jar添加到JAVA Buildpath,但我成了运行时错误:E / AndroidRuntime(736):java.lang.NoClassDefFoundError:org.jsoup.Jsoup
How can I resolve this error? 我该如何解决这个错误?
I encountered this exact problem after a recent update of the ADT component of Android. 最近更新了Android的ADT组件后,我遇到了这个确切的问题。 It looks like Android is ignoring the build path and instead using the ANT defaults. 看起来Android忽略了构建路径,而是使用ANT默认值。
I solved this by removing my jar file from the build path, creating a folder in the "root" of the app heirarchy (alongside src, gen, etc) called "libs", and putting my .jar there. 我通过从构建路径中删除我的jar文件解决了这个问题,在应用程序heirarchy的“根”中创建了一个文件夹(与src,gen等一起),称为“libs”,并将我的.jar放在那里。 When you clean / build and re-launch the app, the error should go away. 当您清理/构建并重新启动应用程序时,错误应该消失。
FYI - this is occurring because the JAR file is not getting packaged into the .apk files - this is why it builds correctly, but isn't available during run-time on your Android device. 仅供参考 - 出现这种情况是因为JAR文件未打包到.apk文件中 - 这就是它正确构建的原因,但在Android设备的运行时无法使用。
see NoClassDefFoundError - Eclipse and Android 请参阅NoClassDefFoundError - Eclipse和Android
还有一个问题,jar必须在libs(带有's')目录而不是lib ....这可能是你的问题吗?
I solved the same problem by following above steps 我按照上面的步骤解决了同样的问题
You don't need the line: 你不需要这条线:
<uses-library android:name = "org.jsoup.Jsoup"/>
in your manifest file. 在您的清单文件中。 All you need to do to use Jsoup is to ensure it's part of your build path by doing the following: 使用Jsoup所需要做的就是通过执行以下操作来确保它是构建路径的一部分:
Then, Jsoup will act like any other library in java. 然后,Jsoup将像java中的任何其他库一样运行。 For example in my app I use it like so: 例如在我的应用程序中我使用它如下:
try
{
Document doc = Jsoup.connect( myHtml ).get();
Elements table = doc.select( "table#ctl00_ContentPlaceHolder1_tblPasses" );
// Returns first row of the table which we want
Elements row = table.select( "tr.lightrow" );
Element record_One = row.first();
}
Note - I have not included all my code for it because it's rather long. 注意 - 我没有包含我的所有代码,因为它相当长。 Anyway, after that you just import the classes as needed, for example in mine I use the following: 无论如何,在那之后你只需要根据需要导入类,例如在我的中使用以下内容:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
You are doing it wrong. 你做错了。 You cant use Jsoup functions in the oncreate of Activity. 你不能在Activity的oncreate中使用Jsoup函数。 You need to make either Async Task or thread. 您需要创建异步任务或线程。
This is to prevent doing blocking tasks in you activity. 这是为了防止在您的活动中执行阻止任务。
Here is a nice blog that explain how to do it and provide sample code as well. 这是一个很好的博客,解释了如何做到这一点并提供示例代码。
I was able to solve this problem by following: 我能够通过以下方式解决这个问题:
Just close the Eclipse and open it again. 关闭Eclipse并再次打开它。 The problem will be solved. 问题将得到解决。
My solution was 我的解决方案是
ps I was using jsoup and sherlock. ps我正在使用jsoup和sherlock。 I didn'y change sherlock library, only my main project. 我没有改变sherlock库,只有我的主要项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.