Android FinDeviceWBYID返回null

Android FinDeviceWBYID返回null,android,Android,我正试图基于hello地图教程构建一个简单的地图应用程序,但是使用findViewById获得的MapView返回空值。但是,地图在模拟器中正确显示。因此,该应用程序可以工作,但我无法在代码中检索地图视图。我错过了什么 代码: 布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" andro

我正试图基于hello地图教程构建一个简单的地图应用程序,但是使用
findViewById
获得的
MapView
返回空值。但是,地图在模拟器中正确显示。因此,该应用程序可以工作,但我无法在代码中检索地图视图。我错过了什么

代码:

布局:

<?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)
应该在
MapView MapView=(MapView)findViewById(R.id.MapView)之前出现

@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//
setContentView(R.layout.main)
必须添加到
MapView
之前

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MapView mapView = (MapView) findViewById(R.id.mapview);
        if (mapView == null) {
            Log.d("onCreate", "map is null"); //log shows that mapView is null
        }
    }

我建议您在呼叫后立即添加
setContentView
super.onCreate(savedInstanceState)

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         setContentView(R.layout.main); //<----------------------------------------

        MapView mapView = (MapView) findViewById(R.id.mapview);
        if (mapView == null) {
            Log.d("onCreate", "map is null"); //log shows that mapView is null
        }

    }
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MapView mapView = (MapView) findViewById(R.id.mapview);
        if (mapView == null) {
            Log.d("onCreate", "map is null"); //log shows that mapView is null
        }
    }