只有創建視圖層次結構的原始線程才能訪問它的視圖

[英]Only the original thread that created a view hierarchy can touch its views


I'm trying to get an ImageView to animate between 2 images in my drawable folder.

我想讓一個ImageView在我的可畫文件夾中的兩個圖像之間動畫。

I thought everything would work fine, but the log is showing an error: Only the original thread that created a view hierarchy can touch its views.

我認為一切都會很好,但是日志顯示了一個錯誤:只有創建了視圖層次結構的原始線程才能觸及它的視圖。

Heres my code:

這是我的代碼:

public class ExerciseActivity extends Activity {
    private ExercisesDataSource datasource;
    private Cursor cursor;
    private ImageView image_1_view;
    private Timer _timer;
    private int _index;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exercise);     

        datasource = new ExercisesDataSource(this);
        datasource.open();

        cursor = datasource.fetchExercise(exerciseDataID);

        image_1_view = (ImageView) findViewById(R.id.exercise_image);

        _index = 1;
        _timer = new Timer();
        _timer.schedule(new TickClass(), 1000);

    }

    public class TickClass extends TimerTask
    {
        private int columnIndex;

        @Override
        public void run() {
            if (_index == 1) {
                columnIndex = cursor.getColumnIndex(MySQLiteHelper.COLUMN_IMAGE_1);
                _index = 2;
            }
            else {
                columnIndex = cursor.getColumnIndex(MySQLiteHelper.COLUMN_IMAGE_2);
                _index = 1; 
            }   

            String image_1 = cursor.getString(columnIndex);
            image_1 = image_1.replace(".png", "");
            int resourceId = getResources().getIdentifier(getPackageName() + ":drawable/" + image_1, null, null);
            image_1_view.setImageDrawable(getResources().getDrawable(resourceId));
        }
    }
}

I went ahead and set the classes and functions to public but that did not fix it.

我將類和函數設置為public,但這並沒有修復它。

All of the resources and everything are fine, how do I fix this error?

所有的資源和一切都很好,我如何修正這個錯誤?

2 个解决方案

#1


36  

Your code in TickClass runs on another thread. To do UI work from here use runOnUiThread. See the docu for details.

TickClass中的代碼在另一個線程上運行。要從這里做UI工作,請使用runOnUiThread。看記錄片。

runOnUiThread(new Runnable() {
    public void run() {
        image_1_view.setImageDrawable(getResources().getDrawable(resourceId));
    }
});

#2


3  

You should use a Handler. http://developer.android.com/reference/android/os/Handler.html

您應該使用一個處理程序。http://developer.android.com/reference/android/os/Handler.html

Create the handler in the onCreate. Then use the handler in with the other thread. Wrap the code in within the post and this code will be executed on the UI thread which may change the UI components.

在onCreate中創建處理程序。然后將處理程序與其他線程一起使用。將代碼封裝在post中,該代碼將在可能更改UI組件的UI線程上執行。

"public final boolean post (Runnable r) Added in API level 1

在API級別1中添加的公共最終布爾值(Runnable r)

Causes the Runnable r to be added to the message queue. The runnable will be run on the thread to which this handler is attached."

使可運行的r被添加到消息隊列中。runnable將在該處理程序所附加的線程上運行。

    handler.post( new Runnable() {

        @Override
        public void run() {
            image_1_view.setImageDrawable(getResources().getDrawable(resourceId));
        }
    });

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/01/12/72545ce10586be63fede137e03c9fea9.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com