java - Android 谷歌地图,移动标记崩溃应用程序

标签 java google-maps android-studio

这是代码“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);

    }

/* 一旦可用就操纵 map 。 本地图准备好使用时会触发此回调。 这是我们可以添加标记或线条、添加监听器或移动摄像机的地方。在这种情况下, 我们只是在澳大利亚悉尼附近添加了一个标记。 如果设备上没有安装 Google Play 服务,系统会提示用户安装 它在 SupportMapFragment 中。只有在用户有 安装了 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();

    }
}

一旦 tryes 执行此行,应用程序就会崩溃:

                    marker.setPosition(NextPosition);

谁能告诉我问题出在哪里? 谢谢!

最佳答案

发生崩溃是因为您没有在 UIThread 中运行此代码。

您可以使用 runOnUiThread 并为 Runnable() 实现第二个强制性 run() 方法以与您的 TimerTask 一起工作。

尝试将 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);
}

另见相关主题:
java.lang.IllegalStateException: Not on the main thread Google Maps
Android Add Map Marker Error: java.lang.IllegalStateException: Not on the main thread

希望这对您有所帮助!

关于java - Android 谷歌地图,移动标记崩溃应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57448972/

相关文章:

java - 使用来自不同类的 Java 枚举?

ios - 有什么方法可以同步 Map Vector(OpenGL 层)和 UIKit?

java - 为什么我在 Android Studio 中收到此错误 "Expected resource of type raw"?

安卓工作室: "Gradle is running. Proceed with Project closing?"

java - 根据单选按钮选择更改/替换 fragment

java - 实现 Remove 方法 Java

jquery - 在AJAX上初始化Google map 刷新部分

android - Google Map API 无法插入到包含 Google Map Json 的 URL 中

java - 使用 Android Studio 在 Save 上运行单元测试

java - 对于用户数据库的“Use In Activity_main”类型,未定义 getactivity() 方法