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

时间:2015-06-02 15:28:06

标签: android android-layout timertask

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

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

}

有什么问题?

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

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

由于

1 个答案:

答案 0 :(得分:0)

以这种方式尝试:

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