private Timer mTimer = new Timer(true);
private TimerTask mTimerTask;
private ImageSwitcher switcher;
//图片索引
private int index = 0;
//显示的图片资源
private static final String TAG = "timeActivity";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); final int[] imageResourese = new int[]{R.drawable.sunset,R.drawable.water,R.drawable.winter};
switcher = (ImageSwitcher) findViewById(R.id.imageSwitchId); switcher.setFactory(this);
switcher.setImageResource(R.drawable.sunset);
try {
mTimerTask = new TimerTask()
{
public void run()
{
index ++;
if (index >= imageResourese.length){
index = 0;
}
Log.i(TAG, String.valueOf(index));
switcher.setImageResource(R.drawable.water); //运行这里报错了,不知道为什么,请解答
}
};
} catch (Exception e) {
}
mTimer.schedule(mTimerTask, 5000,3000);
}定时切换图片时报错,如果用两个按钮,点上一张,下一张的就不会报错。
出现的问题好像是不是同一个线程之中。谢谢了。
private TimerTask mTimerTask;
private ImageSwitcher switcher;
//图片索引
private int index = 0;
//显示的图片资源
private static final String TAG = "timeActivity";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); final int[] imageResourese = new int[]{R.drawable.sunset,R.drawable.water,R.drawable.winter};
switcher = (ImageSwitcher) findViewById(R.id.imageSwitchId); switcher.setFactory(this);
switcher.setImageResource(R.drawable.sunset);
try {
mTimerTask = new TimerTask()
{
public void run()
{
index ++;
if (index >= imageResourese.length){
index = 0;
}
Log.i(TAG, String.valueOf(index));
switcher.setImageResource(R.drawable.water); //运行这里报错了,不知道为什么,请解答
}
};
} catch (Exception e) {
}
mTimer.schedule(mTimerTask, 5000,3000);
}定时切换图片时报错,如果用两个按钮,点上一张,下一张的就不会报错。
出现的问题好像是不是同一个线程之中。谢谢了。
1 :使用handler
2 : runOnUiThread()使用这个方法可以实现