android,无法使用计时器

发布于 2024-12-01 07:36:23 字数 1139 浏览 2 评论 0原文

我想改变每秒帧布局的背景图像。对于此任务,我使用计时器和计时器任务类,但它似乎不起作用,因为初始背景永远不会改变,并且我测试以下代码的物理设备异常终止。

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

┼── 2024-12-08 07:36:23

您应该在设置新的背景资源后调用invalidate()

You should call invalidate() after setting the new background resource.

岁月如刀 2024-12-08 07:36:23

您无法从计时器等非 UI 线程访问视图。您需要有一个处理程序来更新视图并让计时器向其发送消息。你需要阻止 i 出界,例如:

TimerTask myTimerTask = new TimerTask() {
    public void run() {
        Message m = Message.obtain();
        m.what = i;
        myUpdateHandler.sendMessage(m);
        i++;
        if (i >= myList.size())
            i = 0;
    }
};

Handler myUpdateHandler = new Handler() {
    /** Gets called on every message that is received */
    @Override
    public void handleMessage(Message msg) {
        fl.setBackgroundResource(myList.get(msg.what));

    }
};

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:

TimerTask myTimerTask = new TimerTask() {
    public void run() {
        Message m = Message.obtain();
        m.what = i;
        myUpdateHandler.sendMessage(m);
        i++;
        if (i >= myList.size())
            i = 0;
    }
};

.

Handler myUpdateHandler = new Handler() {
    /** Gets called on every message that is received */
    @Override
    public void handleMessage(Message msg) {
        fl.setBackgroundResource(myList.get(msg.what));

    }
};

.

自演自醉 2024-12-08 07:36:23

好吧,您的代码有几个问题。
从您的代码中不清楚“fi”是在哪里初始化的,是在调用计时器回调之前吗?后?
int“i”的用途是什么?不应该是班级成员吗?
您必须在 Activity 的 onDestroy 上停止计时器,否则在访问框架布局时可能会出现一些不良行为。

无论如何,尝试从 onCreate 运行以下命令:

final FrameLayout fl = (FrameLayout)findViewById(R.id.frameLayout1); // You must have final here
final List<Integer> myList = <get it from where you need to>
int i = 0; // What is the purpose of this int? it passed by value to the callback - are you sure it is needed?
TimerTask myTimerTask = new TimerTask()
{
    public void run()
    {
        fl.setBackgroundResource(myList.get(i));
        i++; // Shouldn't it be a class member?
    }
};

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:

final FrameLayout fl = (FrameLayout)findViewById(R.id.frameLayout1); // You must have final here
final List<Integer> myList = <get it from where you need to>
int i = 0; // What is the purpose of this int? it passed by value to the callback - are you sure it is needed?
TimerTask myTimerTask = new TimerTask()
{
    public void run()
    {
        fl.setBackgroundResource(myList.get(i));
        i++; // Shouldn't it be a class member?
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文