Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have another problem i can't solve by myselve.

My situation: I have four FrameLayouts in LinearLayouts.

enter image description here

I color them, set Layout hight and width at wrap content and just change the minwidth to use them as a status or progress bar.

To adjust the bars i have a method:

public void update_bars(){

    FrameLayout bar_eat = (FrameLayout)findViewById(R.id.bar_eat);
    FrameLayout bar_drink = (FrameLayout)findViewById(R.id.bar_drink);
    FrameLayout bar_wash = (FrameLayout)findViewById(R.id.bar_wash);
    FrameLayout bar_care = (FrameLayout)findViewById(R.id.bar_care);

    bar_eat.setMinimumWidth((int)(((float)screen_part/1000)*eat));
    bar_drink.setMinimumWidth((int)(((float)screen_part/1000)*drink));
    bar_wash.setMinimumWidth((int)(((float)screen_part/1000)*wash));
    bar_care.setMinimumWidth((int)(((float)screen_part/1000)*care));
}

This method works fine from everywhere in my activity. But i also want to call this update_bars() method every second so i made a timer for it.

private void update(){

    Timer timer = new Timer();
    timer.schedule(new TimerTask(){

        @Override
        public void run() {
            update_bars()

        }   

    },0,(1000* getResources().getInteger(R.integer.remove_speed_inSecond)));

But at this point it crashes....

The debugger says this:

Thread [<12> Timer-0] (ViewRootImpl$CalledFromWrongThreadException)
Timer$TimerImpl.run() Zeile: 290

So where it the problem? And what can i do? I woud be very happy if someone have an idear because i'm going crazy^^

share|improve this question

closed as too localized by Darth Satan, Maik Semder, bummzack, Sean Middleditch, Byte56 Apr 4 '13 at 22:22

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Voting to close as "too localized"; this is a "please debug my code for me" question. –  Darth Satan Mar 30 '13 at 23:18
    
Right, it was an question linke this. But what shoud i do else if i can't figurout the problem? –  besnep Mar 31 '13 at 11:21

1 Answer 1

up vote 1 down vote accepted

Timer creates new thread. You can't update UI from non UI thread. Use Handler, AsyncTask or runOnUiThread method.

share|improve this answer
    
Thank you sooooo much :) runOnUiThread worked for me. Quit simple^^ :) –  besnep Mar 31 '13 at 0:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.