0

我编写了一个从 Internet 读取数据的 AsyncTask 扩展类。尽管代码在模拟器(GB)中运行良好,即;它正在从互联网返回数据,但是当我在手机 (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
    }

}

日志猫:

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): 在 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) 07-15 18:51:36.378: E/StrictMode(1466): at java.lang.Thread.run(Thread.java:856)

问候

4

1 回答 1

0

您正在尝试在后台线程上做受限的事情。JellyBean 在执行此规则方面变得更加严格,而 GingerBread 则没有。这就是为什么你的模拟器没有问题,但它在你的设备上坏了。将 startActivity(intent) 和任何其他 UI 交互代码移至 onPostExecute,或使用处理程序将消息从 AsyncTask 发布到主线程。您真的只想在 AsyncTask 中执行后台操作和可能的阻塞操作。希望这可以帮助

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

于 2013-07-15T17:05:57.883 回答