Feona
Feona

Reputation: 295

Null pointer exception in the webview

I am getting a null pointer exception in the line where setJavaScriptEnabled(true) is set true. I did not get the exception when I implemented the same program as the only activity in my application, but now i am using an intent from another activity and getting to this(google) activity, i am getting Null pointer Exception. Please tell where am i going wrong.

public class Google extends Activity {
    /** Called when the activity is first created. */
    WebView mWebView;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.google.com");
        mWebView.setWebViewClient(new HelloWebViewClient());

    }

    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }




}

Upvotes: 1

Views: 4926

Answers (2)

Rahmathullah M
Rahmathullah M

Reputation: 2716

You might have set a wrong layout here..

setContentView(R.layout.main); // < ------ You might have set a wrong layout here.

Make sure that your WebView is main.xml.

Upvotes: 2

Peter Ilfrich
Peter Ilfrich

Reputation: 3816

Try debugging:

if (mWebView == null) { System.out.print("mWebView is null"); }
else if (mWebView.getSettings() == null) { System.out.print("Settings is null"); }

Probably

findViewById(R.id.webview)

does not return a WebView as you expect.

Upvotes: 0

Related Questions