Splash screen - changing widget properties during loop
I'm using a pretty standard loop with sleep that increments by开发者_如何学JAVA 100 milliseconds each time, but I want to add a text color change halfway through... However, I get an error. Is there a problem in the following code?
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;
}
}
You must not update UI elements (TextView
in your case) from a background thread. See the article about UI and threading on Android.
精彩评论