Question

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.

Était-ce utile?

La solution

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

Autres conseils

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

    }
};

.

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?
    }
};
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top