这个Bug发生在这样的环境: 你有很多的线程在同时运行,并且每个线程都需要持续运行相当长一些时间; 其中一些是用AsyncTask去作的,并且AsyncTask在其他的线程执行顺序之后。
java.lang.
NoClassDefFoundError: android
.os.AsyncTask
.这里暂时不贴出异常栈信息(出现在uncaughtException),通过大量的search,终于找到一些有营养的资料:
Received: by 10.101.85.19 with SMTP id n19mr4167516anl.30.1314658242480;
Mon, 29 Aug 2011 15:50:42 -0700 (PDT)
X-BeenThere: android
-developers@googlegroups.com
Received: by 10.101.181.21 with SMTP id i21ls13340762anp.4.gmail; Mon, 29 Aug
2011 15:48:59 -0700 (PDT)
Received: by 10.101.24.5 with SMTP id b5mr2043100anj.37.1314658139241;
Mon, 29 Aug 2011 15:48:59 -0700 (PDT)
Received: by 10.151.138.5 with SMTP id q5msybn;
Mon, 29 Aug 2011 10:44:15 -0700 (PDT)
Received: by 10.101.168.1 with SMTP id v1mr728239ano.36.1314639855438;
Mon, 29 Aug 2011 10:44:15 -0700 (PDT)
Date: Mon, 29 Aug 2011 10:44:14 -0700 (PDT)
From: exakoustos <stratouk...@gmail.com>
Reply-To: android
-developers@googlegroups.com
To: android
-developers@googlegroups.com
Message-ID: <28014072.5885.1314639854428.JavaMail.geo-discussion-forums@yqgc10>
In-Reply-To: <3208001.1407.1314283401379.JavaMail.geo-discussion-forums@yqcd38>
References: <0146c2a8-4093-4416-b2c4-d0698109cb95@a12g2000yqi.googlegroups.com>
<CANCScgjEZ1YEHL+R9srFEkoA7wft_s=LzDpcmLnyHWTa_NM2_A@mail.gmail.com> <3abc056a-a09d-4a64-a341-a1064cd988ee@z17g2000vbp.googlegroups.com>
<CALLdnhPdcfScNw7cScT_fcKKSR-S08qjGtbyVTb-JrQjwkbd2g@mail.gmail.com>
<3208001.1407.1314283401379.JavaMail.geo-discussion-forums@yqcd38>
Subject: Re: [android
-developers] Re: java.lang.
NoClassDefFoundError:
android
.os.AsyncTask
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_5883_18950387.1314639854427"
------=_Part_5883_18950387.1314639854427
Content-Type: multipart/alternative;
boundary="----=_Part_5884_488802.1314639854427"
------=_Part_5884_488802.1314639854427
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Was faced with the same error when trying to launch an AsyncTask and found
that the reason it would fail was that an unrelated thread would throw
an unhanded exception early on. It seems the AsyncTask and the other thread
(used to fetch ad banners) shared resources and when the ad thread
misbehaved it made the
class
loader misbehave as well.
Managed to figure this out while reading your post after obsessing for 4
days. Thank you!
------=_Part_5884_488802.1314639854427
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit
Was faced with the same error when trying to launch an AsyncTask and found that the reason it would fail was that an unrelated thread would throw an unhanded exception early on. It seems the AsyncTask and the other thread (used to fetch ad banners) shared resources and when the ad thread misbehaved it made the class
loader misbehave as well.<div><br></div><div>Managed to figure this out while reading your post after obsessing for 4 days. Thank you!</div>
------=_Part_5884_488802.1314639854427--
------=_Part_5883_18950387.1314639854427--
上面的东西还能大致看懂: 由于其他线程和AsyncTask在装载时都在竞争相同的资源,导致AsyncTask竞争失败,进一步导致class loader装载它失败。
怎么会这样呢, 想不通?!
查看源代码 http://www.oschina.net/code/explore/android-2.2-froyo/android/os/AsyncTask.java
Line 184: Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
再看: http://www.oschina.net/code/explore/android-2.2-froyo/android/os/Process.java
Line 126: public
static
final
int
THREAD_PRIORITY_LOWEST =
19
;
Line 136: public
static
final
int
THREAD_PRIORITY_BACKGROUND =
10
;
看完后真的感到无可奈何! 调整线程优先级看来没辙了。
继续奇想: 让class loader优先装载它如何?! 了解过class loader就知道(Class.forName): 如果一个class被装载过一次,以后就不会再出先类似:
java.lang.
NoClassDefFoundError
.
到此,问题就解决了。 直接在Application#onCreate里靠前面的位置执行一个空的AsyncTask实例就解决问题。
如果你能确保你自己的线程顺序,把AsyncTask放在当前Activity里的其他线程之前也可以。但是没有上面的方法保险。
终于松了口气! 也真明白了线程优先级的危害。
分享到:
相关推荐
整个系统都没有错误,但是在发布运行后,控制台上却抛出了这样的异常: java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
主要介绍了Android 出现:Android出现:java.lang.NoClassDefFoundError: android/os/PersistableBundle错误解决办法的相关资料,需要的朋友可以参考下
今天测试突然给我说我写的XX界面一点app就crash了! 纳尼,我肯定表示不服啊!... W/System.err: java.lang.NoClassDefFoundError: android/os/PersistableBundle W/System.err: at java.lang.Class.getDeclar
MyEclipse+SSH错误:java.lang.NoClassDefFoundError orgobjectwebasmCodeVisitor解决.rar
NULL 博文链接:https://lyndon-lin.iteye.com/blog/856002
java.lang.NoClassDefFoundError: com/sun/activation/registries/LogSupport异常处理
java.lang.NoClassDefFoundError: de/javakaffee/kryoserializers/CurrencySerializer
jdk升级之后出现异常:java.lang.NoClassDefFoundError: javax/xml/ws/Service 1、解决方案jdk降级至1.8之前 2、由于JDK9以后,去除了javax.xml包需要引入对应的jar包 <groupId>javax.activation <artifactId>...
该jar包解决报错java.lang.NoClassDefFoundError: org/apache/james/mime4j/MimeException,亲测可用。
Caused by: java.lang.NoClassDefFoundError: com.sun.org.apache.xml.internal.resolver.CatalogManager at com.sun.xml.ws.util.xml.XmlUtil.createEntityResolver(XmlUtil.java:266) at org.jvnet.jax_ws_...
在jsp运行过程中,报错 java.lang.NoClassDefFoundError org.apache.xml.serializer.TreeWalker时的处理方法。
包含3个jar(修改后的javaee.jar可以接覆盖原jar,和mail.jar和activition.jar)和一个文档,测试通过。 JavaMail的java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream错误 原因: MyEclipse...
java解决Handler processing failed; nested exception is java.lang.NoClassDefFoundError
NULL 博文链接:https://bijian1013.iteye.com/blog/2305804
jdk升级jdk10后,原本jdk自带的 webservice一些包确实,引起的一系列错误解决方案
jackson-annotations-2.10.2.jar jackson-core-2.10.2.jar jackson-databind-2.10.2.jar NoClassDefFoundError解决
NULL 博文链接:https://hnychenl.iteye.com/blog/675625
druid出现 java.lang.NoClassDefFoundError: org/apache/log4j <!-- <groupId>log4j <artifactId>log4j <version>1.2.17 </dependency>--> <groupId>org.slf4j <artifactId>log4j-over-slf4j ...
解决java.lang.NoClassDefFoundError这个问题,直接导入这几个包就解决了