Android Google maps, Moving Marker crash application

Alessandro :

This is the code "MapsActiity.java":

package com.example.myapplicationgooglemaps;

import ...


public class MapsActivity extends FragmentActivity implements
        OnMapReadyCallback {

    private static GoogleMap mMap;
    private static Marker marker;

    private static int x = -34;
    private static int y = 151;

    private static LatLng NextPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }


    public void myTimer() {
        Timer t = new Timer();

        t.schedule(new TimerTask() {
            @Override
            public void run() {

                if (mMap != null) {

                    x = x + 1;
                    y = y + 1;

                    NextPosition = new LatLng(x, y);

                    marker.setPosition(NextPosition);
                    mMap.moveCamera(CameraUpdateFactory.newLatLng(NextPosition));
                    mMap.animateCamera(CameraUpdateFactory.zoomTo(16f));

                }
            }


        }, 2000, 1000);

    }

/* Manipulates the map once available. This callback is triggered when the map is ready to be used. This is where we can add markers or lines, add listeners or move the camera. In this case, we just add a marker near Sydney, Australia. If Google Play services is not installed on the device, the user will be prompted to install it inside the SupportMapFragment. This method will only be triggered once the user has installed Google Play services and returned to the app. */

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng position = new LatLng(x, y);
        marker = mMap.addMarker(new MarkerOptions().position(position).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(position));

        myTimer();

    }
}

App crash as soon as tryes executes this line:

                    marker.setPosition(NextPosition);

Who can explain me where is the problem ? Thank you!

evan :

The crash is happening because you are not running this code in the UIThread.

You can use runOnUiThread and implement a second mandatory run() method for Runnable() to work with your TimerTask.

Try replacing your code within myTimer() with the following:

public void myTimer() {
    Timer t = new Timer();

    t.schedule(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run () {
                    if (mMap != null) {

                        x = x + 1;
                        y = y + 1;

                        NextPosition = new LatLng(x, y);

                        marker.setPosition(NextPosition);
                        mMap.moveCamera(CameraUpdateFactory.newLatLng(NextPosition));
                        mMap.animateCamera(CameraUpdateFactory.zoomTo(16f));
                    }
                }
            });
        }
    }, 2000, 1000);
}

Also see related threads:
java.lang.IllegalStateException: Not on the main thread Google Maps
Android Add Map Marker Error: java.lang.IllegalStateException: Not on the main thread

Hope this helps!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=161943&siteId=1