1
\$\begingroup\$

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^^

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

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

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thank you sooooo much :) runOnUiThread worked for me. Quit simple^^ :) \$\endgroup\$ – Spenhouet Mar 31 '13 at 0:31

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