jacobohunter
jacobohunter

Reputation: 393

How to rotate pictures of a background of layout in android through java in a timer?

I'm trying to have the relationship.xml have its background changed from one image to another. It starts off as the red image and its supposed to change to the cosmos image after the timer and then go to the red after the timer again. The app seems to crash when I use this code but when I comment out the statement in the else statement, the app does not crash. Any ideas?

boolean setred;
RelativeLayout layout;
Thread timer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(starting.rt.R.layout.relationship);
    layout = (RelativeLayout) findViewById(starting.rt.R.id.relativelayout1);
    setred = false;

timer = new Thread() {

    public void run() {
        try {
            sleep(7000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (setred) {

                setred = false;
                layout.setBackgroundResource(starting.rt.R.drawable.cosmos);
            } else {
                setred = true;
                layout.setBackgroundResource(starting.rt.R.drawable.red);

            }

        }
    }
};
timer.start();

This is the logcat

06-17 16:23:08.347: E/AndroidRuntime(657): FATAL EXCEPTION: Thread-10
06-17 16:23:08.347: E/AndroidRuntime(657): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
06-17 16:23:08.347: E/AndroidRuntime(657):  at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
06-17 16:23:08.347: E/AndroidRuntime(657):  at android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
06-17 16:23:08.347: E/AndroidRuntime(657):  at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
06-17 16:23:08.347: E/AndroidRuntime(657):  at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
06-17 16:23:08.347: E/AndroidRuntime(657):  at android.view.View.invalidate(View.java:5279)
06-17 16:23:08.347: E/AndroidRuntime(657):  at android.view.View.setBackgroundDrawable(View.java:7626)
06-17 16:23:08.347: E/AndroidRuntime(657):  at android.view.View.setBackgroundResource(View.java:7535)
06-17 16:23:08.347: E/AndroidRuntime(657):  at starting.rt.Base$1.run(Base.java:56)
06-17 16:23:08.386: W/ActivityManager(75):   Force finishing activity starting.rt/.RelationshipTipsActivity

Upvotes: 2

Views: 623

Answers (1)

zozelfelfo
zozelfelfo

Reputation: 3776

Watching the logCat I would say that the problem here is that you are creating the views in one thread an modifying them in another.

Try to use runOnUiThread

Found a similar problem here

Edit:

I'd try this way

super.onCreate(savedInstanceState);
setContentView(starting.rt.R.layout.relationship);
layout = (RelativeLayout) findViewById(starting.rt.R.id.relativelayout1);
setred = false;

timer = new Thread() {

public void run() {
    try {
        sleep(7000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        MyActivity.this.runOnUiThread(new Runnable() {

        public void run() {
          if (setred) {
            setred = false;
            layout.setBackgroundResource(starting.rt.R.drawable.cosmos);
          } else {
            setred = true;
            layout.setBackgroundResource(starting.rt.R.drawable.red);
          }
        }
    });
  }
 }
};
timer.start();

Note that layout var should be final to be final in order to be referenced.

Upvotes: 2

Related Questions