I’m trying to build a simple Maps app based on the hello map tutorial but the MapView obtained with findViewById returns null. However, the map is shown properly in the emulator. So the app works, but I can’t retrieve the map view in the code. What am I missing?
Code:
public class HelloMapActivity extends MapActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MapView mapView = (MapView) findViewById(R.id.mapview);
if (mapView == null) {
Log.d("onCreate", "map is null"); //log shows that mapView is null
}
setContentView(R.layout.main);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="the key"
/>
</RelativeLayout>
setContentView(R.layout.main)must be added before yourMapView.I recommend to you add
setContentViewalways immediately after you callsuper.onCreate(savedInstanceState).