Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a tab in which I have a WebView. I have initialised the WebView in onCreateView method and wrote below code but getting error
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference


Java Code:

Java
public class Tab3 extends Fragment {
    WebView web;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tab_2, container, false);
        web=(WebView)v.findViewById(R.id.webView1);
        // Force links and redirects to open in the WebView instead of in a browser
        //web.setWebViewClient(new WebViewClient());
        web.loadUrl("http://www.google.com");
        return v;
    }
}


XML Code :

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


    <WebView
        android:id="@+id/webView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </WebView>

</RelativeLayout>
Posted
Updated 1-Sep-15 21:39pm
v2
Comments
Richard MacCutchan 2-Sep-15 3:42am    
You need to use your debugger to see why the call to findViewById(R.id.webView1) is not returning a valid reference.
bhavikadb 3-Sep-15 1:00am    
Can you guide me for the same I'm very new to Android
Richard MacCutchan 3-Sep-15 3:03am    
Then I suggest you go to http://www.codeproject.com/KB/android/#Android+Tutorial+Contest and work through some of the samples.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900