2011-09-01 106 views
1

我有一些奇怪的问题,与MapView。findViewById返回null从布局查看

<?xml version="1.0" encoding="utf-8"?> 
<com.google.android.maps.MapView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mapview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:clickable="true" 
    android:apiKey="my-api-key"/> 

mapView = (MapView)findViewById(R.id.mapview);对我而言返回null。但MapView显示正常。我做错了什么?

UPD:有我的课

public class WayMapActivity extends MapActivity { 

private MapView mapView = null; 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.maplayout); 

    mapView = (MapView)findViewById(R.id.mapview); 
    mapView.setBuiltInZoomControls(true); 
} 

@Override 
public void onResume(){ 
    super.onResume(); 

} 

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

调用*后的setContentView该行*()? – Blundell

+0

有时候通过清理项目 – drewi

+0

来解决。另外,你的类是否扩展了MapActivity? – drewi

回答

0

你叫setContentView(layout)findViewById过吗?

然后尝试清理并重建您的项目。有时需要重建R

+0

尝试清理并重建您的项目。有时需要重建R –

+0

谢谢。清洁项目对我有帮助。 – KoirN

+0

我很高兴它有帮助。这样的问题可以杀死很多时间,并造成脑损伤:) –

0

这种情况偶尔会发生在我身上。

  1. 删除根文件夹
  2. 做一个项目,清洁
  3. 重办