Android 在 AsyncTask 中设置复选框 setChecked() 方法出错?

Android setting checkbox setChecked() method in AsyncTask gives error?

我想在 AsyncTask 中生成我的 UI(因为生成几乎需要一秒钟)。 我拿到了数据,然后在后台生成所有的UI元素和布局,在onPostExecute我把完成的UI布局添加到contentFrameLayout.

FrameLayout contentFrameLayout;  // this is a FrameLayout in the BaseActivity

private class loadUiAsync extends AsyncTask<List<MyData>, Void, LinearLayout>{
        LinearLayout mainLayout;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog();

            mainLayout =  ( LinearLayout ) LayoutInflater.from(MainActivity.this).inflate(R.layout.content_layout, null, false);
        }

        @Override
        protected LinearLayout doInBackground( List<MyData>... datas ) {

            // I create and generate a lot's of views from the datas, and add them to the mainLayout.
            checkBox.setChecked(/* TRUE or FALSE value based on data */);          // ----> THIS CAUSES THE CRASH

            return mainLayout;
        }

         @Override
        protected void onPostExecute( LinearLayout mainLayout ) {
            super.onPostExecute(mainLayout);

            contentFrameLayout.removeAllViews();
            contentFrameLayout.addView(mainLayout);

            dismissDialog();
        }
}

这在 android 10 上工作正常,但在 android 5 和 6 上崩溃:

03-18 10:01:11.443 11598-12560/com.my.app E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4
    Process: com.my.app, PID: 11598
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask.done(AsyncTask.java:309)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:234)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
     Caused by: android.util.AndroidRuntimeException: Animators may only be run on Looper threads
        at android.animation.ValueAnimator.start(ValueAnimator.java:1069)
        at android.animation.ValueAnimator.start(ValueAnimator.java:1117)
        at android.animation.ObjectAnimator.start(ObjectAnimator.java:852)
        at android.animation.AnimatorSet.start(AnimatorSet.java:586)
        at android.animation.AnimatorSet.start(AnimatorSet.java:586)
        at android.animation.AnimatorSet.start(AnimatorSet.java:586)
        at androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat.start(AnimatedVectorDrawableCompat.java:719)
        at androidx.appcompat.graphics.drawable.AnimatedStateListDrawableCompat$AnimatedVectorDrawableTransition.start(AnimatedStateListDrawableCompat.java:439)
        at androidx.appcompat.graphics.drawable.AnimatedStateListDrawableCompat.selectTransition(AnimatedStateListDrawableCompat.java:346)
        at androidx.appcompat.graphics.drawable.AnimatedStateListDrawableCompat.onStateChange(AnimatedStateListDrawableCompat.java:280)
        at android.graphics.drawable.Drawable.setState(Drawable.java:680)
        at android.widget.CompoundButton.drawableStateChanged(CompoundButton.java:464)
        at androidx.appcompat.widget.AppCompatCheckBox.drawableStateChanged(AppCompatCheckBox.java:224)
        at android.view.View.refreshDrawableState(View.java:17061)
        at android.widget.CompoundButton.setChecked(CompoundButton.java:145)
        at com.my.app.view.ui.MainActivity$loadUiAsync.doInBackground(MainActivity.java:396)
        at com.my.app.view.ui.MainActivity$loadUiAsync.doInBackground(MainActivity.java:153)
        at android.os.AsyncTask.call(AsyncTask.java:295)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:234) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
        at java.lang.Thread.run(Thread.java:818) 
Caused by: android.util.AndroidRuntimeException: Animators may only be run on Looper threads

你可以试试runOnUiThread

Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.

runOnUiThread(new Runnable() {
            @Override
            public void run() {
                 checkBox.setChecked(true/false);
            }
});