2

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
MDMalik
  • 3,951
  • 2
  • 25
  • 39
jacobohunter
  • 363
  • 1
  • 4
  • 16

1 Answers1

2

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.

Community
  • 1
  • 1
zozelfelfo
  • 3,776
  • 2
  • 21
  • 35
  • Ok I'm not too familiar with that method or threads. I see what you're getting at, I just can't seem to implement it into my code. – jacobohunter Jun 17 '13 at 17:02
  • Check the edited answer I think I've mispelled some brackets but it should be something like that – zozelfelfo Jun 17 '13 at 17:16
  • ok yes your code has no errors, but the picture does not change now. Would it matter if this class is being extended by different classes? I'm going to go ahead accept yours as an answer if no one else has a solution by the end of today. I appreciate your time in helping me figure it out. There must be an issue of how I have things set up as the app does not crash now but does not change pictures. – jacobohunter Jun 17 '13 at 18:43
  • The problem might be in the setred var check it out for the correct values. – zozelfelfo Jun 17 '13 at 19:31