Null Pointer Exception of WebView in Android
this NullPointerException i am getting makes no sense to me. I have a webview that was 开发者_运维技巧just working fine for a long time and now it is showing null for no reason i can see.
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>
as you can see it is defined properly and referenced properly but browser is null when i trace it in the debugger. It cant be the html file because i know its there and i didnt change anything with it. so why is it giving me this?
I assume your XML is located in a res/layout* directory appropriate for your current device configuration. Might want to clean and rebuild to make sure the Android resources are in sync with your code.
精彩评论