我正在尝试以固定的时间间隔更改活动的背景。这是我的活动代码
public class ExploreActivity extends Activity {
private int randNum = 0 ;
private int [] colorID = {R.color.AliceBlue,R.color.Beige,R.color.YellowGreen};
private Random rand = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_explore);
final Handler handler = new Handler(getMainLooper(), new Callback() {
@Override
public boolean handleMessage(Message msg) {
// TODO Auto-generated method stub
getWindow().setBackgroundDrawableResource(colorID[msg.arg1]);
return true;
}
});
final Message msg = new Message();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
randNum = rand.nextInt(colorID.length);
msg.arg1 = randNum;
handler.sendMessageAtFrontOfQueue(msg);
}
}, 5000, 5000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.explore, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.action_logout:{
SharedPreferences settings = getSharedPreferences("settings", MODE_PRIVATE);
Editor editor = settings.edit();
editor.putBoolean("login_state", false);
editor.commit();
finish();
}
default:
return super.onOptionsItemSelected(item);
}
}
}
我收到这个错误,颜色变化只发生一次然后这个错误来了
07-01 08:03:28.364: E/AndroidRuntime(3104): FATAL EXCEPTION: main
07-01 08:03:28.364: E/AndroidRuntime(3104): java.lang.IllegalStateException: The specified message queue synchronization barrier token has not been posted or has already been removed.
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.os.MessageQueue.removeSyncBarrier(MessageQueue.java:266)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.os.Looper.removeSyncBarrier(Looper.java:242)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:981)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.view.Choreographer.doFrame(Choreographer.java:532)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.os.Handler.handleCallback(Handler.java:725)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.os.Handler.dispatchMessage(Handler.java:92)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.os.Looper.loop(Looper.java:137)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-01 08:03:28.364: E/AndroidRuntime(3104): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 08:03:28.364: E/AndroidRuntime(3104): at java.lang.reflect.Method.invoke(Method.java:511)
07-01 08:03:28.364: E/AndroidRuntime(3104): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-01 08:03:28.364: E/AndroidRuntime(3104): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-01 08:03:28.364: E/AndroidRuntime(3104): at dalvik.system.NativeStart.main(Native Method)
请帮助我以适当的方式做这件事