How to add webView in android studio

i made a swipe tab app ,where when you swipe you can see tabs. On tab 2 i want to add a webView but when i add webview on tab 2 add and this code [Note: i tried every tutorial on youtube and stackoverflow but it didn’t work i don’t know where to put java code]

WebView view = (WebView) this.findViewById(R.id.tab2wv);
    view.getSettings().setJavaScriptEnabled(true);
    view.loadUrl("http://www.google.com/");

on my MainActivity.java my app crashes and it says unfortunately, app_name is stopped.

here is MainActivity.java

package com.example.free;


import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;




ViewPager pager;
PagerTabStrip tab_strp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_tab);
ma_pager_adapter mapager = new ma_pager_adapter(getSupportFragmentManager());
pager = (ViewPager) findViewById(R.id.pager);

pager.setAdapter(mapager);
tab_strp = (PagerTabStrip) findViewById(R.id.tab_strip);
tab_strp.setTextColor(Color.WHITE);
//   tab_strp.setTextSize(14,14);
// tab_strp.setTabIndicatorColor(Color.WHITE);



    WebView view = (WebView) this.findViewById(R.id.tab2wv);
    view.getSettings().setJavaScriptEnabled(true);
    view.loadUrl("http://www.google.com/");

}

here is tab2.xml

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


<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tab2wv"
android:layout_gravity="center" />

here is manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.free" >

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
    android:name="com.example.free.MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.