AsyncTask的doInBackground不运行 [英] AsyncTask doInBackground does not run

查看:130
本文介绍了AsyncTask的doInBackground不运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用的AsyncTask类的问题。这似乎是我的任务将停止创建4个或5个任务后,工作。

I'm having a problem with the AsyncTask class. It seems like my task stops working after creating 4 or 5 tasks.

林有2活动。 MainActivity只认为,启动了一个名为ImageActivity。第二个活动按钮

Im having 2 activities. MainActivity which only holds a button that starts a second activity called ImageActivity.

ImageActivity是很简单的。它得到了一个的onCreate,设置布局,然后开始一个新的AsyncTask加载图像从互联网上。这工作正常,前几次。但比它突然停止工作。上preExecute方法运行每次,但不是doInBackground方法。我试图简化doInBackground带睡眠循环,同样的事情发生。我无法理解这水煤浆由于AsyncTask的既取消,设置为空的的onDestroy方法。所以每次我开始一个新的ImageActivity,我也创造了一个新的AsyncTask的。

ImageActivity is very simple. it got an onCreate that sets the layout, and then it starts a new AsyncTask that loads an image from the internet. This works fine the first few times. But than it suddenly stops working. The onPreExecute method is run every time, but not the doInBackground method. I have tried to simplify the doInBackground with a sleeping loop, and the same thing happens. I cant understand this behavour since the asynctask is both canceled and set to null in the onDestroy method. So every time i start a new ImageActivity, i also create a fresh AsyncTask.

我重新创建ImageActivity,并通过点击返回按钮,比点击MainActivity按钮的任务。

I recreate the ImageActivity and the task by hitting the back button, and than clicking the button on the MainActivity.

任何想法吗?我真的很挣扎与这一个。

Any ideas anyone? I'm really struggling with this one.

更新:启动该ImageActivity(内部按钮onClickListener)code

UPDATE: Code that starts the ImageActivity (inside a button onClickListener)

Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.setClassName(this, ImageActivity.class.getName());
startActivity(intent);

在code以上开始这项活动

The code above starts this activity

    public class ImageActivity extends Activity {

    private AsyncTask<Void, Void, Void> task;

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.main);

        task = new AsyncTask<Void, Void, Void>() {

            @Override
            protected void onPreExecute()
            {
                Log.d(TAG, "onPreExecute()");
            }

            @Override
            protected Void doInBackground(Void... params)
            {
                Log.d(TAG, "doInBackground() -- Here is the download");
                // downloadBitmap("http://mydomain.com/image.jpg")
                return null;
            }

            @Override
            protected void onPostExecute(Void res)
            {
                Log.d(TAG, "onPostExecute()");
                if(isCancelled()){
                    return;
                }
            }
        }.execute();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        task.cancel(true);
    }
}

更新:

我用传统的线程和runOnUiThread方法的组合已经测试,它似乎更好地工作。现在线程运行的每一次。

I have tested using a combination of traditional Threads and runOnUiThread method, and it seems to work better. Now the thread runs every time.

推荐答案

删除AsyncTask的,并使用传统主题,而不是结合的 runOnUiThread 似乎是工作。但我还没有找到为什么AsyncTask的是如此的不稳定的原因

Removing the AsyncTask and using a traditional Thread instead combined with runOnUiThread seems to be work. But I still have not found the reason why the AsyncTask is so "unstable"

下面是code,它为我的作品:

Here is the code that works for me:

public class ImageActivity extends Activity {

    private Thread worker;

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.main);

        worker = new Thread(new Runnable(){

            private void updateUI(final List<Object> list)
            {
                if(worker.isInterrupted()){
                    return;
                }
                runOnUiThread(new Runnable(){

                    @Override
                    public void run()
                    {
                        // Update view and remove loading spinner etc...
                    }
                });
            }

            private List<Object> download()
            {
                // Simulate download
                SystemClock.sleep(1000);
                return new ArrayList<Object>();
            }

            @Override
            public void run()
            {
                Log.d(TAG, "Thread run()");
                updateUI(download());
            }

        });
        worker.start(); }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        worker.interrupt();
    }
}

这篇关于AsyncTask的doInBackground不运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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