0

我想从列表中删除一个对象,同时我想为用户制作一个淡出动画......

删除函数创建一个Thread,在线程中我尝试启动动画,但我得到了那个异常:

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that      created a view hierarchy can touch its views.

关于活动:

private Animation animation;
private AnimationListener al;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    animation = AnimationUtils.loadAnimation(this, R.anim.fade_out);                
    al = new AnimationListener() {

        public void onAnimationStart(Animation animation) {
        // do nothing       
        }

        public void onAnimationRepeat(Animation animation) {
        // do nothing       
        }

        public void onAnimationEnd(Animation animation) {
            TableRow tr = (TableRow) findViewById(R.id.test);                   
            tr.setVisibility(View.GONE);
        }

    };

    animation.setAnimationListener(al);                             
    animation.reset();            
}

当用户按下删除图标时,他将到达这里:

public void remove(View v) {         
    RemoveF rf = new RemoveF();
    rf.start();
}

我的胎面从这里开始:

class RemoveF extends Thread {
    private boolean running;

    public void run() {
        running = true;
        try {
            do {
                //business logic goes here
                TableRow tr = (TableRow) findViewById(R.id.test);
                tr.setAnimation(animation);
                tr.startAnimation(animation);
                stopRunning();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ie) {
                    // do nothing
                }
            } while (running);
        } catch (Exception e) {
            Log.e("RemoveF", "Exception", e);
        }
    }

    public void stopRunning() {
        running = false;
    } 
}

知道我该怎么做吗?谢谢

4

1 回答 1

2

这里TableRow tr = (TableRow) findViewById(R.id.test);

您正在尝试从另一个线程访问 UI 元素。

使用runOnUiThreadorHandler用于从线程更新 UI

于 2012-05-11T18:58:54.843 回答