AsyncTask生成RuntimeException

时间:2013-07-15 17:02:52

标签: java android sql android-asynctask

我编写了一个AsyncTask扩展类,它从Internet读取数据。虽然代码在模拟器(GB)中工作正常但是i-e;它从互联网返回数据,但是当我在手机(JB)上运行代码时,它不会返回任何内容。此外,代码在emualtor的logcat中创建以下异常。这是代码:

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 com.android.emailcommon.service.ServiceProxy$ProxyConnection@b50afe10 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 com.android.emailcommon.service.ServiceProxy$ProxyConnection@b50afe10 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 com.android.emailcommon.service.ServiceProxy$ProxyConnection@b50afe10 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):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 07-15 18:51:36.378:E / StrictMode(1466):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573) 07-15 18:51:36.378:E / StrictMode(1466):at java.lang.Thread.run(Thread.java:856)

此致

1 个答案:

答案 0 :(得分:0)

您正在尝试在后台线程上执行受限制的操作。 JellyBean对执行此规则的要求更加严格,而GingerBread则没有。这就是为什么你的模拟器上没有问题,但它在你的设备上破坏了。将startActivity(intent)和任何其他UI交互式代码移动到onPostExecute,或者使用Handler将消息从AsyncTask发布到主线程。您真的只想在AsyncTask中执行后台操作并可能阻止操作。希望这有帮助

编辑:原来你试图在doInBackground()中显示Toast。这也是不允许的,因为您正在尝试与要在主线程上触及的视图进行交互。删除那个toast,然后使用Log或使用Handler实例来显示它。