2011-03-26 72 views
0

這個NullPointerException我得到的對我來說毫無意義。我有一個很長時間以來工作得很好的webview,現在它顯示爲空,因爲我看不到任何理由。Android中WebView的空指針異常

public class SmsMain extends Activity { 

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

    WebView browser = (WebView) findViewById(R.id.webView2); //debugger says browser is null 
    browser.loadUrl("file:///android_asset/smsabout.html"); //error is here 
} 

XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_height="fill_parent" android:layout_width="match_parent"> 

    <WebView android:id="@+id/webView2" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"></WebView> 

</LinearLayout> 

,你可以看到它是正確定義和正確引用,但是當我在調試跟蹤它的瀏覽器爲空。它不能是HTML文件,因爲我知道它在那裏,我沒有改變任何東西。那麼爲什麼它給了我這個?

回答

1

我假設您的XML位於適合您當前設備配置的res/layout *目錄中。可能需要清理並重新編譯,以確保Android資源與您的代碼同步。