1

What are possible error when I encountered the NullPointerException for Webview:

WebView webView;
webView = (WebView) findViewById(R.id.webView3);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("...");

This is my webview3.xml:

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView3"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

NPE is on line webView.getSettings().setJavaScriptEnabled(true);

3
  • Where exactly do you get a null pointer exception? Commented Mar 18, 2013 at 12:16
  • at which line do you get NPE Commented Mar 18, 2013 at 12:16
  • on line webView.getSettings().setJavaScriptEnabled(true); Commented Mar 18, 2013 at 12:21

3 Answers 3

3

May be missing to load the xml layout webview3.xml ie. setContentView(R.layout.webview3.xml);

Sign up to request clarification or add additional context in comments.

Comments

3

Try this sample code,

res/layout/webview3.xml

<?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"> 

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

</LinearLayout> 

set permission in AndroidManifest.xml file

<uses-permission android:name="android.permission.INTERNET" />

And use this code in your onCreate() method.

setContentView(R.layout.webview3.xml);
WebView mywebview = (WebView) findViewById(R.id.webview);
mywebview.loadUrl("http://Enter your URL here");
WebSettings webSettings = mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);

Comments

0

Another way:

    View webViewLayout = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
            .inflate(R.layout.web_main, null, false);
    WebView webView = (WebView) webViewLayout.findViewById(R.id.webview);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.