2

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!

Alessandro
  • 23
  • 2
  • 1
    If the app crashes you should have a stacktrace somewhere. Add that to your question. Also node that according to the `Java Code Conventions` variable names should start with a lower case character. – second Aug 11 '19 at 10:23
  • E/BufferQueueProducer: [] Can not get hwsched service D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, egl_color_buffer_format *, EGLBoolean) returns 0x3000 W/InputMethodManager: startInputReason = 5 I/zygote64: Do full code cache collection, code=117KB, data=106KB I/zygote64: After code cache collection, code=114KB, data=72KB A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0xc in tid 21897 (Timer-0) Disconnected from the target VM, address: 'localhost:8602', transport: 'socket' – Alessandro Aug 11 '19 at 20:10
  • A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0xc in tid 21897 (Timer-0) Disconnected from the target VM, address: 'localhost:8602', transport: 'socket' – Alessandro Aug 11 '19 at 20:17
  • E/AndroidRuntime: FATAL EXCEPTION: Timer-0 Process: com.example.myapplicationgooglemaps, PID: 22668 com.google.maps.api.android.lib6.common.apiexception.d: Not on the main thread at com.google.maps.api.android.lib6.common.l.b(:com.google.android.gms.dynamite_mapsdynamite@18382051@18.3.82 (040408-260264002):6) – Alessandro Aug 11 '19 at 20:32
  • You should edit you queston and add the `stacktrace` over there, instead of posting it into the comments. Its quite hard to read it in the comments, but it does not look like its complete? – second Aug 12 '19 at 11:51

1 Answers1

0

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!

evan
  • 5,443
  • 2
  • 11
  • 20