简体   繁体   English

Android Google Maps,Moving Marker崩溃应用程序

[英]Android Google maps, Moving Marker crash application

This is the code "MapsActiity.java": 这是代码“ 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. 如果设备上未安装Google Play服务,则会提示用户将其安装在SupportMapFragment中。 This method will only be triggered once the user has installed Google Play services and returned to the app. 仅当用户安装了Google Play服务并返回到应用程序后,才会触发此方法。 */ * /

    @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! 谢谢!

The crash is happening because you are not running this code in the UIThread. 发生崩溃是因为您没有在UIThread中运行此代码。

You can use runOnUiThread and implement a second mandatory run() method for Runnable() to work with your TimerTask. 您可以使用runOnUiThread并为Runnable()实现第二个强制性run()方法,以与TimerTask一起使用。

Try replacing your code within myTimer() with the following: 尝试用以下代码替换myTimer()代码:

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 java.lang.IllegalStateException:不在主线程Google Maps上
Android Add Map Marker Error: java.lang.IllegalStateException: Not on the main thread Android添加地图标记错误:java.lang.IllegalStateException:不在主线程上

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM