2013-07-15 85 views
0

我寫了一個AsyncTask擴展類,它從互聯網上讀取數據。儘管代碼在仿真器(GB)i-e中工作正常,它從互聯網返回數據,但是當我在手機(JB)上運行代碼時,它不會返回任何內容。此外,該代碼正在創建以下例外情況在logual的emualtor。下面是代碼:AsyncTask生成RuntimeException

new userCred().execute(userCredentials); 

public class userCred extends AsyncTask<String[], String, String> 
{ 

    @Override 
    protected String doInBackground(String[]... arg0) { 

     ...... 

     startActivity(intent); 

     return null; // because I am not returning anythign 
    } 

} 

logcat的:

07-15 21:49:56.768: W/System.err(1542): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
07-15 21:49:56.768: W/System.err(1542):  at android.os.Handler.<init>(Handler.java:121) 
07-15 21:49:56.768: W/System.err(1542):  at android.widget.Toast.<init>(Toast.java:68) 
07-15 21:49:56.768: W/System.err(1542):  at android.widget.Toast.makeText(Toast.java:231) 
07-15 21:49:56.768: W/System.err(1542):  at com.umer.androiduetproject.LoginAccount.sendCredentialsForCheckingOnline(LoginAccount.java:71) 
07-15 21:49:56.768: W/System.err(1542):  at com.umer.androiduetproject.LoginAccount$userCred.doInBackground(LoginAccount.java:124) 
07-15 21:49:56.768: W/System.err(1542):  at com.umer.androiduetproject.LoginAccount$userCred.doInBackground(LoginAccount.java:1) 
07-15 21:49:56.768: W/System.err(1542):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
07-15 21:49:56.768: W/System.err(1542):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
07-15 21:49:56.768: W/System.err(1542):  at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
07-15 21:49:56.768: W/System.err(1542):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
07-15 21:49:56.768: W/System.err(1542):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
07-15 21:49:56.768: W/System.err(1542):  at java.lang.Thread.run(Thread.java:1019) 
07-15 21:50:24.169: W/System.err(1542): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
07-15 21:50:24.169: W/System.err(1542):  at android.os.Handler.<init>(Handler.java:121) 
07-15 21:50:24.169: W/System.err(1542):  at android.widget.Toast.<init>(Toast.java:68) 
07-15 21:50:24.169: W/System.err(1542):  at android.widget.Toast.makeText(Toast.java:231) 
07-15 21:50:24.169: W/System.err(1542):  at com.umer.androiduetproject.LoginAccount.sendCredentialsForCheckingOnline(LoginAccount.java:71) 
07-15 21:50:24.169: W/System.err(1542):  at com.umer.androiduetproject.LoginAccount$userCred.doInBackground(LoginAccount.java:124) 
07-15 21:50:24.169: W/System.err(1542):  at com.umer.androiduetproject.LoginAccount$userCred.doInBackground(LoginAccount.java:1) 
07-15 21:50:24.169: W/System.err(1542):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
07-15 21:50:24.179: W/System.err(1542):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
07-15 21:50:24.179: W/System.err(1542):  at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
07-15 21:50:24.179: W/System.err(1542):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
07-15 21:50:24.179: W/System.err(1542): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
07-15 21:50:24.179: W/System.err(1542): at java.lang.Thread.run(Thread.java:1019) 

JB的logcat:

07-15 18:51:36.378: E/ActivityThread(1466): Service com.android.exchange.ExchangeService has leaked ServiceConnection [email protected]afe10 that was originally bound here 
07-15 18:51:36.378: E/ActivityThread(1466): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection [email protected]afe10 that was originally bound here 
07-15 18:51:36.378: E/ActivityThread(1466):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969) 
07-15 18:51:36.378: E/ActivityThread(1466):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 
07-15 18:51:36.378: E/ActivityThread(1466):  at android.app.ContextImpl.bindService(ContextImpl.java:1418) 
07-15 18:51:36.378: E/ActivityThread(1466):  at android.app.ContextImpl.bindService(ContextImpl.java:1407) 
07-15 18:51:36.378: E/ActivityThread(1466):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473) 
07-15 18:51:36.378: E/ActivityThread(1466): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 
07-15 18:51:36.378: E/ActivityThread(1466):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) 
07-15 18:51:36.378: E/ActivityThread(1466):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191) 
07-15 18:51:36.378: E/ActivityThread(1466):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850) 
07-15 18:51:36.378: E/ActivityThread(1466):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551) 
07-15 18:51:36.378: E/ActivityThread(1466):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549) 
07-15 18:51:36.378: E/ActivityThread(1466):  at android.os.AsyncTask$2.call(AsyncTask.java:287) 
07-15 18:51:36.378: E/ActivityThread(1466):  at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
07-15 18:51:36.378: E/ActivityThread(1466):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
07-15 18:51:36.378: E/ActivityThread(1466):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
07-15 18:51:36.378: E/ActivityThread(1466):  at java.lang.Thread.run(Thread.java:856) 
07-15 18:51:36.378: E/StrictMode(1466): null 
07-15 18:51:36.378: E/StrictMode(1466): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection [email protected]afe10 that was originally bound here 
07-15 18:51:36.378: E/StrictMode(1466):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969) 
07-15 18:51:36.378: E/StrictMode(1466):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 
07-15 18:51:36.378: E/StrictMode(1466):  at android.app.ContextImpl.bindService(ContextImpl.java:1418) 
07-15 18:51:36.378: E/StrictMode(1466):  at android.app.ContextImpl.bindService(ContextImpl.java:1407) 
07-15 18:51:36.378: E/StrictMode(1466):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473) 
07-15 18:51:36.378: E/StrictMode(1466):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 
07-15 18:51:36.378: E/StrictMode(1466):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) 
07-15 18:51:36.378: E/StrictMode(1466):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191) 
07-15 18:51:36.378: E/StrictMode(1466):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850) 
07-15 18:51:36.378: E/StrictMode(1466):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551) 
07-15 18:51:36.378: E/StrictMode(1466):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549) 
07-15 18:51:36.378: E/StrictMode(1466):  at android.os.AsyncTask$2.call(AsyncTask.java:287) 
07-15 18:51:36.378: E/StrictMode(1466):  at java.util.concurrent.FutureTask.run(FutureTask.java:234) 

07-15 18:51:36.378:E/StrictMode(1466):在java的。 util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 07-15 18:51:36.378:E/StrictMode(1466):在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573)一十月7日至15日18:51:36.378:E/StrictMode(1466):在java.lang.Thread.run(Thread.java:856)

問候

回答

0

你試圖在做事情的限制後臺線程。果凍豆在執行這個規則方面已經變得更加嚴格了,薑餅餅乾不是。這就是爲什麼你的模擬器沒有問題,但它在你的設備上壞了。相反,將startActivity(intent)和任何其他UI交互式代碼移動到onPostExecute,或者使用Handler從AsyncTask將消息發佈到主線程。你真的只是想在AsyncTask中做背景和潛在的阻塞操作。希望這可以幫助

編輯:原來你試圖在doInBackground()中顯示Toast。這也是不允許的,因爲你正在嘗試與主要線程中要涉及的視圖進行交互。刪除該吐司,並使用日誌,或使用Handler實例來顯示它。

+0

好吧,現在我做了什麼,我正在閱讀doBackground()中的互聯網數據並在onPostExecute()中開始新的活動。但是我仍然得到了上述例外,現在新的活動還沒有開始! –

+0

請發佈你的AsyncTask的完整代碼 –

+0

http://pastebin.com/h6DNHt8g –

相關問題