如何显示土司的AsyncTask在Android的 [英] How to Display toast in AsyncTask in android

查看:144
本文介绍了如何显示土司的AsyncTask在Android的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的initial_bakcground类扩展的AsyncTask我在logcat中收到此错误显示Taost。

I am Trying to display Taost in my initial_bakcground class extended with AsyncTask i am receiving this error in logcat.

public class InitialBackgroundTask extends AsyncTask<URL, Integer, Long> {

@Override
protected Long doInBackground(URL... params) {
    // TODO Auto-generated method stub
    show a = new show();
    a.loop();
    return null;
}

public class show {

void loop()
{
    for(int i=0; i<10; i++)
    {
        Toast.makeText(MainActivity.me, "test", Toast.LENGTH_LONG).show();
    }
}
}


05-30 12:08:12.641: E/AndroidRuntime(30840): FATAL EXCEPTION: AsyncTask #1
05-30 12:08:12.641: E/AndroidRuntime(30840): java.lang.RuntimeException: An error occured while executing doInBackground()
05-30 12:08:12.641: E/AndroidRuntime(30840):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
05-30 12:08:12.641: E/AndroidRuntime(30840):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-30 12:08:12.641: E/AndroidRuntime(30840):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-30 12:08:12.641: E/AndroidRuntime(30840):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-30 12:08:12.641: E/AndroidRuntime(30840):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-30 12:08:12.641: E/AndroidRuntime(30840):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
05-30 12:08:12.641: E/AndroidRuntime(30840):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-30 12:08:12.641: E/AndroidRuntime(30840):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-30 12:08:12.641: E/AndroidRuntime(30840):    at java.lang.Thread.run(Thread.java:856)
05-30 12:08:12.641: E/AndroidRuntime(30840): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-30 12:08:12.641: E/AndroidRuntime(30840):    at android.os.Handler.<init>(Handler.java:121)
05-30 12:08:12.641: E/AndroidRuntime(30840):    at android.widget.Toast$TN.<init>(Toast.java:317)
05-30 12:08:12.641: E/AndroidRuntime(30840):    at android.widget.Toast.<init>(Toast.java:91)
05-30 12:08:12.641: E/AndroidRuntime(30840):    at android.widget.Toast.makeText(Toast.java:233)
05-30 12:08:12.641: E/AndroidRuntime(30840):    at   com.example.toast.show.loop(show.java:11)
05-30 12:08:12.641: E/AndroidRuntime(30840):    at com.example.toast.InitialBackgroundTask.doInBackground(InitialBackgroundTask.java:13)
05-30 12:08:12.641: E/AndroidRuntime(30840):    at com.example.toast.InitialBackgroundTask.doInBackground(InitialBackgroundTask.java:1)
05-30 12:08:12.641: E/AndroidRuntime(30840):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
05-30 12:08:12.641: E/AndroidRuntime(30840):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-30 12:08:12.641: E/AndroidRuntime(30840):    ... 5 more

以上code正显示出整个故事。其实,我想告诉土司在doInBackground方法

The above code is showing the whole story. Actually, I want to show toast in doInBackground method

推荐答案

您无法更新UI上的后台线程。 doinBAckground上调用后台线程。你应该在UI线程上更新用户界面。

You cannot update ui on the background thread. doinBAckground is invoked on the background thread. You should update ui on the ui thread.

      runOnUiThread(new Runnable(){

          @Override
          public void run(){
            //update ui here
            // display toast here 
          }
       });

在preExecute(),onPostExecute(结果),在UI线程上被调用。所以,你可以在这里显示敬酒。

onPreExecute(),onPostExecute(Result), are invoked on the ui thread. So you can display toast here.

onProgressUpdate(进展...),调用publishProgress(进展......)之后的UI线程调用。可用于动态文本字段进度条或显示日志。

onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). can be used to animate a progress bar or show logs in a text field.

doInbackground的结果()计算是一个参数onPostExecute(结果),所以返回的结果中doinBackground(),并显示在onPostExecute(结果),你的面包

The result of doInbackground() computation is a parameter to onPostExecute(Result) so return the result in doinBackground() and show your toast in onPostExecute(Result)

您还可以使用一个处理程序所建议的@Stine派克

You can also use a handler as suggested by @Stine Pike

有关清晰检查下面的主题下的4个步骤的链接。

For clarity check the link below under the topic The 4 steps.

<一个href="http://developer.android.com/reference/android/os/AsyncTask.html">http://developer.android.com/reference/android/os/AsyncTask.html

这篇关于如何显示土司的AsyncTask在Android的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆