`

解决一个棘手的bug: java.lang.NoClassDefFoundError: android.os.AsyncTask

阅读更多

           这个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&nbsp;unhanded&nbsp; 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里的其他线程之前也可以。但是没有上面的方法保险。

         终于松了口气! 也真明白了线程优先级的危害。

 

3
0
分享到:
评论
1 楼 myreligion 2013-04-10  
nice job!

相关推荐

Global site tag (gtag.js) - Google Analytics