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
4

1 回答 1

2

观看 logCat 我会说这里的问题是您在一个线程中创建视图并在另一个线程中修改它们。

尝试使用runOnUiThread

在这里发现了类似的问题

编辑:

我会尝试这种方式

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

请注意,layout var 应该是 final 才能被引用。

于 2013-06-17T16:33:20.883 回答