2011-09-30 41 views
1

我有以下佈局。Android:findViewById返回NULL - 無法找出什麼是錯的?

當我在我的MainActivity類中創建WebViewGestureOverlayView對象時,它將爲這兩個對象返回null

GestureOverlayView gesturesView = (GestureOverlayView) findViewById(R.id.gestures); 
WebView webView = (WebView) findViewById(R.id.webview); 

我的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<android.gesture.GestureOverlayView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gestures" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <WebView 
      android:id="@+id/webview" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 
    </LinearLayout> 
</android.gesture.GestureOverlayView> 

當我改變上述佈局下述(i移動android.gesture.GestureOverlayView內部LinearLayout),它工作正常。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <android.gesture.GestureOverlayView 
     android:id="@+id/gestures" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <WebView 
      android:id="@+id/webview" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 
    </android.gesture.GestureOverlayView> 
</LinearLayout> 

我無法弄清楚我以前的佈局有什麼問題。

請有人解釋這一點。

+3

您是否在使用findViewById之前調用過setContentView? – Sephy

+0

是的......當然。 – gtiwari333

+1

然後一旦清理項目 – ingsaurabh

回答

1

圍繞Android資源的許多問題都可以通過清理和重建項目來解決。