我要添加按钮,将在地图视图普通和卫星视图之间切换和我不断收到一个应用程序已意外停止
错误
公共类NewMapsActivity扩展MapActivity{ 私人MapView类myMapView; @覆盖 保护无效的onCreate(捆绑冰柱) { super.onCreate(冰柱); 的setContentView(R.layout.main); MapView的myMapView =(图形页面)findViewById(R.id.mapview); 按钮mySatelliteButton =(按钮)findViewById(R.id.c_button); mySatelliteButton.setOnClickListener(新View.OnClickListener() { 公共无效的onClick(视图v) { SetSatellite(); } }); myMapView.setBuiltInZoomControls(真); } 私人无效SetSatellite() { 如果(myMapView.isSatellite()) myMapView.setSatellite(假); 其他 myMapView.setSatellite(真); } @覆盖 保护布尔isRouteDisplayed() { 返回false; }}
解决方案
如果一切赋予的权利,然后检查空指针异常是否发生在myMapview对象。既然你已经宣布在顶部,然后里面的onCreate您使用的是图形页面mymapView =(图形页面)findViewById(R.id.mapview);所以,当你使用内部setStallite mymapview对象()这将只需要这是在顶部声明的对象。那么试试这个
myMapView =(图形页面)findViewById(R.id.mapview);
但你在顶部,图形页面myMapview声明myMapview;
检查这个如果不是请在发布您的logcat中误差
I want to add button which will toggle between normal and satellite view in the map view and I keep getting a the application has stopped unexpectedly
error
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;
}
}
解决方案
If everything is given right then check whether nullpointer exception occur in myMapview object. since you have declared at top then inside onCreate you are using MapView mymapView=(MapView) findViewById(R.id.mapview); so when you use mymapview object inside setStallite() it will take only the object which was declare at the top. so try this
myMapView = (MapView) findViewById(R.id.mapview);
but you to declare myMapview at top as MapView myMapview;
check this if not please post your error in logcat