3

30秒ごとにループできるようにしたい次のコードがあります。

MyLocation myLocation = new MyLocation();
    public LocationResult locationResult = new LocationResult() {
        @Override
        public void gotLocation(final Location location) {
            GeoPoint myGeoPoint = new GeoPoint(
                    (int) (location.getLatitude() * 1000000),
                    (int) (location.getLongitude() * 1000000));
            myMapController.animateTo(myGeoPoint);
            myMapController.setZoom(10);

            lat = location.getLatitude();
            lon = location.getLongitude();

            new OverlayTask().execute();


            Timer timer;
            timer = new Timer();
            timer.scheduleAtFixedRate(new MyTimerTask(), 30000, 30000); 
        }

    };

場所を取得すると起動し、OverlayTaskは正常に実行されます。また、メニュー項目に割り当てられたときにも実行され、強制的に実行されます。

ただし、タイマー内でエラーが発生します。

public class MyTimerTask extends TimerTask {
    public void run() {
        try {
            Log.i(">>>>>>>>>>>> Refresh: ", "Success");
            new OverlayTask().execute();
        } catch (Exception e) {
            Log.e(">>>>>>>>>>>> Error executing MyAsyncTask: ", e.getMessage(), e);
        }
    }
}

エラーは...です。

MyAsyncTaskの実行エラー:(2573):ビュー階層を作成した元のスレッドのみがそのビューにアクセスできます。

4

1 に答える 1

3

runOnUiThread次の方法を使用します。

public class MyTimerTask extends TimerTask {
    public void run() {
        runOnUIThread(new Runnable(){
            public void run(){
                try {
                    Log.i(">>>>>>>>>>>> Refresh: ", "Success");
                    new OverlayTask().execute();
                } catch (Exception e) {
                    Log.e(">>>>>>>>>>>> Error executing MyAsyncTask: ", e.getMessage(), e);
                }
            }
        });
    }
}
于 2010-08-22T14:52:27.933 に答える