如何修复“NoClassDefFoundError”?我一直在尝试在我的Android应用程序中设置Jsoup。我已将Jsoup导入libs文件夹。 当我编译代码时,它编译然后应用程序中断。
这是主要节日。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.news.scrapweb3"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.news.scrapweb3.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
以下是我尝试显示从Jsoup提取到textview的文本的代码。
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
private Document doc;
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
try{
doc= Jsoup.connect("http://www.google.com/").get();
String title= doc.title();
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(title);
}catch(IOException e){
}
return rootView;
}
}
这是日志猫。
03-13 09:30:24.799: W/dalvikvm(1646): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-13 09:30:24.809: E/AndroidRuntime(1646): FATAL EXCEPTION: main
03-13 09:30:24.809: E/AndroidRuntime(1646): java.lang.NoClassDefFoundError: org.jsoup.Jsoup
03-13 09:30:24.809: E/AndroidRuntime(1646): at com.news.scrapweb3.MainActivity$DummySectionFragment.onCreateView(MainActivity.java:127)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.view.View.measure(View.java:15518)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.view.View.measure(View.java:15518)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.view.View.measure(View.java:15518)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-13 09:30:24.809: E/AndroidRuntime(1646): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.view.View.measure(View.java:15518)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.view.Choreographer.doFrame(Choreographer.java:532)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.os.Handler.handleCallback(Handler.java:725)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.os.Handler.dispatchMessage(Handler.java:92)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.os.Looper.loop(Looper.java:137)
03-13 09:30:24.809: E/AndroidRuntime(1646): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-13 09:30:24.809: E/AndroidRuntime(1646): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 09:30:24.809: E/AndroidRuntime(1646): at java.lang.reflect.Method.invoke(Method.java:511)
03-13 09:30:24.809: E/AndroidRuntime(1646): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-13 09:30:24.809: E/AndroidRuntime(1646): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-13 09:30:24.809: E/AndroidRuntime(1646): at dalvik.system.NativeStart.main(Native Method)
已更新
如果我删除此代码。项目开始工作。
try {
doc = Jsoup.connect("http://www.google.com/").get();
String title = doc.title();
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(title);
} catch (IOException e) {
}
答案 0 :(得分:2)
当我将项目从eclispe迁移到android studio时,我遇到了这个问题。这个解决方案对我有用:
public class YouApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
自2014年12月3日起,发布了构建工具1.0.0-rc1。现在,您需要做的就是在Application类中覆盖它:
public class YouApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
并像这样修改你的build.gradle:
android {
compileSdkVersion 22
buildToolsVersion "23.0.0"
defaultConfig {
minSdkVersion 14 //lower than 14 doesn't support multidex
targetSdkVersion 22
// Enabling multidex support.
multiDexEnabled true
}
}
dependencies {
compile 'com.android.support:multidex:1.0.1'}
参考:link
答案 1 :(得分:1)
答案 2 :(得分:1)
如果您使用的是库jar,而不是仅从其他位置引用它,请尝试将其放在libs文件夹中。之前曾为我工作过几次。