android - 在没有应用程序崩溃的情况下无法使用 MapController

标签 android android-maps android-mapview

我有两个 MapActivities,使用用户当前位置的那个效果很好。当我尝试创建第二个使用硬编码纬度/经度的 MapActivity 时,它会在使用 MapController 设置动画或设置缩放时崩溃。我的代码如下:

package com.breckbus.app;

import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class Yellowroute extends MapActivity {
    MapController mControl;
    GeoPoint GeoP;
    MapView mapV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yellowroute);

        mapV = (MapView) findViewById(R.id.yellowmapView);

        mapV.displayZoomControls(true);
        mapV.setBuiltInZoomControls(true);

        double lat = 39.482547;
        double longi = -106.047699;

        GeoP = new GeoPoint((int) (lat * 1E6), (int) (longi * 1E6));

        mControl.animateTo(GeoP);
        mControl.setZoom(13);

        mapV.invalidate();
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;

    }

} //end of map.java

如果我取出 mControl.animateTo(GeoP) 或 mControl.setZoom(13) 应用程序将运行良好。但是,如果我留下任何一个,我会得到以下信息:

http://imgur.com/t3eg7

我正在拔头发...知道我做错了什么吗?

最佳答案

你的controller是null,你需要添加一行来实例化它

mapV = (MapView) findViewById(R.id.yellowmapView);
mControl = mapV.getController(); // < You need to add this line

关于android - 在没有应用程序崩溃的情况下无法使用 MapController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9946442/

相关文章:

Android - 使用 DOM 解析器的 KML 解析器

java - 如何将csv文件中的数据解析到容器中并搜索1个值并在java中检索其行上的其余值

android - 为什么这个 BindingAdapter 在 Kotlin 中不起作用?

android - 如何在 ui 线程上注入(inject)并等待注入(inject)完成后再继续?

android - Google Play 开发者建议平板电脑

ios - 自定义标注 View 按钮到 map

java - 当网络服务中的纬度和经度发生变化时,如何移动谷歌地图中的标记?

android - 从另一个、距离和极角确定一个地理点

android - 为通过 GeoPoints 移动的图标设置动画

android - fragment 类中的谷歌地图 onClick 事件