Android-Google Maps Satellite切换

时间:2011-06-30 04:11:10

标签: android google-maps

我想添加一个按钮,该按钮将在地图视图中的普通视图和卫星视图之间切换,并且我不断收到the application has stopped unexpectedly错误

public class NewMapsActivity extends MapActivity 
{    
    private MapView myMapView;

    @Override
        protected void onCreate(Bundle icicle) 
    {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        MapView myMapView = (MapView) findViewById(R.id.mapview);

        Button mySatelliteButton = (Button)findViewById(R.id.c_button);
        mySatelliteButton.setOnClickListener(new View.OnClickListener()
        {
           public void onClick(View v)
            {
                SetSatellite();
            }
        });
     myMapView.setBuiltInZoomControls(true);
     }

    private void SetSatellite()
        {
            if ( myMapView.isSatellite() )
                myMapView.setSatellite(false);
            else 
                myMapView.setSatellite(true);
        }

    @Override
        protected boolean isRouteDisplayed() 
     {
            return false;
     }
}

1 个答案:

答案 0 :(得分:1)

如果一切正确,则检查myMapview对象中是否发生nullpointer异常。因为你已经在onCreate里面声明了你正在使用MapView mymapView =(MapView)findViewById(R.id.mapview);因此,当您在setStallite()中使用mymapview对象时,它将仅采用在顶部声明的对象。所以试试这个

  myMapView = (MapView) findViewById(R.id.mapview);

但你要将myMapview声明为MapView myMapview;

如果没有检查,请在logcat中发布您的错误