这是我第一次在这里提问 我正在尝试使应用程序启动时TextView文本颜色随时间变化为随机颜色。
这是我尝试过的,但它让我的应用程序崩溃了。
TextView txtLucky;
Random rd;
int r = 0, g = 0, b = 0;
Thread stopper = new Thread();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtLucky = findViewById(R.id.txtLucky);
while (true){
try {
wait(2000);
rd = new Random();
int rdcolor = Color.argb(255, rd.nextInt(256), rd.nextInt(256), rd.nextInt(256));
txtLucky.setTextColor(rdcolor);
} catch (InterruptedException e) {
e.printStackTrace();
}
stopper.start();
}
}
错误日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.max.threadstopper, PID: 14708
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.max.threadstopper/com.example.max.threadstopper.MainActivity}: java.lang.IllegalMonitorStateException: object not locked by thread before wait()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.IllegalMonitorStateException: object not locked by thread before wait()
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:422)
at com.example.max.threadstopper.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:6999)
at android.app.Activity.performCreate(Activity.java:6990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
答案 0 :(得分:0)
文本颜色更改代码放在 runOnUiThread 中。因为你已经开始了一个线程。
runOnUiThread(new Runnable() {
@Override
public void run() {
txtLucky.setTextColor(rdcolor);
}
});
答案 1 :(得分:0)
试试这个
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
rd = new Random();
final int rdcolor = Color.argb(255, rd.nextInt(256), rd.nextInt(256), rd.nextInt(256));
runOnUiThread(new Runnable() {
@Override
public void run() {
txtLucky.setTextColor(rdcolor);
}
});
}
},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
2000);