启动画面 - 在循环期间更改小部件属性

时间:2011-10-09 16:00:36

标签: android splash-screen

我正在使用一个非常标准的睡眠循环,每次增加100毫秒,但我想在中途添加文本颜色变化......但是,我收到一个错误。以下代码中是否存在问题?

public class splashActivity extends Activity {
    TextView tv;
    LinearLayout ll;
    protected boolean _active=true;
    protected int _splashTime=5000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        tv=(TextView)findViewById(R.id.textViewSplash);

        ll=(LinearLayout)findViewById(R.id.linearLayoutSplash);

        Thread splashThread = new Thread(){
            @Override
            public void run() {
                try{
                    int waited=0;
                    while (_active && waited < _splashTime){
                        sleep(100);
                        if(_active){
                            waited += 100;
                        }
                        if(waited >=2500){
                        tv.setTextColor(Color.GREEN);
                         }
                    }
                }
                catch(InterruptedException e){
                }
                finally{
                    finish();
                    startActivity(new Intent("com.kleaverdevelopment.splashTest.SplashTest.mainActivity")); //package.package.package.appName.nextActivity
                    stop();
                }
            }
        };
        splashThread.start();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            _active = false;
        }
        return true;
    }
}

1 个答案:

答案 0 :(得分:1)

您不得从后台线程更新UI元素(在您的情况下为TextView)。请参阅有关UI and threading on Android的文章。