用于在 android 中每秒显示图片的 TimerTask 和 Handler

TimerTask and Handler for showing the pictures every seconds in android

我需要使用 TimerTaskImageView 中显示三张图片。这是我的主要 activity:

public class MainActivity extends ActionBarActivity {
    Timer timer;
    TimerTask timerTask;
    final Handler handler = new Handler();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    protected void onResume() {
        super.onResume();
        startTimer();
    }
    public void startTimer() {
        timer = new Timer();
        initializeTimerTask();
        handler.postDelayed(timerTask, 3000);
    }

    ImageView imageView = (ImageView) findViewById(R.id.imageView1);

    public void initializeTimerTask() {
        timerTask = new TimerTask() {
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        handler.postDelayed(this, 3000);
                        new DownloadImageTask(imageView).execute("http://url/background1.jpg");

                    }
                });
            }
        };
    }

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }
        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }
        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }

}

问题是什么?

正是我需要的每三秒,图像自动更改为另一张图片。

那么,我对这些代码做错了什么?

谢谢

这样试试:

public class MainActivity extends ActionBarActivity {
    final Handler handler = new Handler();
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.imageView1);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initialize();
    }

    public void initialize() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
              /* do what you need to do */
              new     DownloadImageTask(imageView).execute("http://url/background1.jpg");
              /* and here comes the "trick" */
              handler.postDelayed(this, 3000);
           }
        };
        handler.postDelayed(runnable, 3000);
    }

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }
        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }
        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }

}

您还没有在 onCreate 方法中引用您的 ImageView。这可能创建了 NullPointerException.