android,无法使用计时器
我想改变每秒帧布局的背景图像。对于此任务,我使用计时器和计时器任务类,但它似乎不起作用,因为初始背景永远不会改变,并且我测试以下代码的物理设备异常终止。
FrameLayout fl;
List<Integer> myList;
int i = 0;
TimerTask myTimerTask = new TimerTask()
{
public void run()
{
fl.setBackgroundResource(myList.get(i));
i++;
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myList = new ArrayList<Integer>();
myList.add(R.drawable.square1);
myList.add(R.drawable.square2);
myList.add(R.drawable.square3);
myList.add(R.drawable.square4);
myList.add(R.drawable.square5);
myList.add(R.drawable.square6);
myList.add(R.drawable.square7);
myList.add(R.drawable.square8);
myList.add(R.drawable.square9);
myList.add(R.drawable.square10);
myList.add(R.drawable.square11);
myList.add(R.drawable.square12);
fl = (FrameLayout)findViewById(R.id.frameLayout1);
long delay = 1000;
long period = 1000;
Timer t = new Timer();
t.schedule(myTimerTask,delay,period);
}
我哪里失败了? ^^ 预先感谢您的宝贵时间。
I would like to change the background image of a frame layout per second. For this task I use timer and timertask classes but it does not seem to work as the initial background never changes and the pyhsical device that I test the following code terminates abnormally.
FrameLayout fl;
List<Integer> myList;
int i = 0;
TimerTask myTimerTask = new TimerTask()
{
public void run()
{
fl.setBackgroundResource(myList.get(i));
i++;
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myList = new ArrayList<Integer>();
myList.add(R.drawable.square1);
myList.add(R.drawable.square2);
myList.add(R.drawable.square3);
myList.add(R.drawable.square4);
myList.add(R.drawable.square5);
myList.add(R.drawable.square6);
myList.add(R.drawable.square7);
myList.add(R.drawable.square8);
myList.add(R.drawable.square9);
myList.add(R.drawable.square10);
myList.add(R.drawable.square11);
myList.add(R.drawable.square12);
fl = (FrameLayout)findViewById(R.id.frameLayout1);
long delay = 1000;
long period = 1000;
Timer t = new Timer();
t.schedule(myTimerTask,delay,period);
}
Where do I fail ? ^^
Thanks in advance for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该在设置新的背景资源后调用
invalidate()
。You should call
invalidate()
after setting the new background resource.您无法从计时器等非 UI 线程访问视图。您需要有一个处理程序来更新视图并让计时器向其发送消息。你需要阻止 i 出界,例如:
。
。
You can't access a view from a non UI thread like a timer. You need to have a handler to update the view and get the timer to send messages to it. And you need to stop i from going out of bounds, like:
.
.
好吧,您的代码有几个问题。
从您的代码中不清楚“fi”是在哪里初始化的,是在调用计时器回调之前吗?后?
int“i”的用途是什么?不应该是班级成员吗?
您必须在 Activity 的 onDestroy 上停止计时器,否则在访问框架布局时可能会出现一些不良行为。
无论如何,尝试从 onCreate 运行以下命令:
Well, you got several problems on your code.
From your code it doesn't clear where the "fi" is initialized, is it before the timer callback is called? after?
What is the purpose of the int "i"? Shouldn't it be a class member?
You must stop the timer on the onDestroy of the Activity, otherwise you might get some undesired behavior when accessing the frame layout.
Anyway, try running the the following from the onCreate: