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

查看:22
本文介绍了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 从互联网加载图像.这在前几次工作正常.但它突然停止工作.每次都运行 onPreExecute 方法,但不运行 doInBackground 方法.我试图用睡眠循环来简化 doInBackground,但同样的事情发生了.我无法理解这种行为,因为 asynctask 在 onDestroy 方法中被取消并设置为 null.所以每次我开始一个新的 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 内)

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);

上面的代码开始这个活动

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 of combining it with runOnUiThread seems to work. But I still have not found the reason why the AsyncTask is so "unstable".

这是对我有用的代码:

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天全站免登陆