2011-09-01 66 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. 重辦