简体   繁体   中英

Change Text Color time by time in OnCreate

This is my first time asking questions here.
I am trying to make my TextView text color changing to a random color time by time when the application is started.

This is what I tried but it made my app crash.

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();
    }
}

Error Log:

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) 

Text color change code put in runOnUiThread . because you already start one Thread.

  runOnUiThread(new Runnable() {
          @Override
          public void run() {
              txtLucky.setTextColor(rdcolor);
          }
      });

Try this

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM