Android Issues

I have the following code that I want to be able to cycle every 30 seconds.

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

    };

Gets fired when I have a place, and the OverlayTask runs fine, it will also execute when assigned to a menu item to force it.

However, inside the timer, I get an error message.

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

Error:....

An error occurred while executing MyAsyncTask: (2573): only the source thread that created the view hierarchy can touch its views.

+3
source share
1 answer

Use the method 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);
                }
            }
        });
    }
}
+3
source

Source: https://habr.com/ru/post/1760994/


All Articles