我试图以固定的时间间隔更改活动的背景。 这是我活动的代码
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)
Plz帮助我采取适当的方式
答案 0 :(得分:3)
您可以使用AnimationDrawable轻松实现所需内容。只需在AnimationDrawable xml文件中创建颜色列表即可。 并在你的onCreate方法中,将drawable附加到背景并在屏幕获得焦点时启动动画。
示例darwable:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="@color/red" android:duration="600" />
<item android:drawable="@color/blue" android:duration="600" />
</animation-list>
和活动:
AnimationDrawable drawable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View parent = findViewById(R.id.parent);
drawable = (AnimationDrawable) getResources().getDrawable(R.drawable.animation_drawable);
parent.setBackgroundDrawable(drawable);
}
@Override
public void onWindowFocusChanged(boolean gained){
super.onWindowFocusChanged(gained);
if(gained){
drawable.start();
}
}
答案 1 :(得分:0)
作为一项活动,您希望定期更改颜色,建议您在布局的父视图中添加 ID ,并使用findViewById(R.id.parentView)
代替您的{{ 1}}。我相信错误就在那个地方。
答案 2 :(得分:0)
您检查了文件[Link]吗?关于sendMessageAtFrontOfQueue
:
此方法仅用于非常特殊的情况 - 它很容易使消息队列饿死,导致排序问题或出现其他意外的副作用。
您能否检查sendMessage
而不是sendMessageAtFrontOfQueue
是否解决了问题?
答案 3 :(得分:0)
从developer.android.com上的示例中找出解决方案
final Handler handler = new Handler(getMainLooper(), new Callback() {
@Override
public boolean handleMessage(Message msg) {
// TODO Auto-generated method stub
root.setBackgroundResource(imageID[msg.arg1]);
return true;
}
});
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
randNum = rand.nextInt(imageID.length);
Message msg = handler.obtainMessage(1, randNum, 0);
msg.sendToTarget();
}
}, 5000, 8000);