title: How to open a url in webview on new screen based on button click
body: <p>Hi i have tried all available solutions in stackoverflow but nothing is working, app is stopped when the button is clicked.  Please help me out.</p>
  
  <p>Whenever i click the button, the app crashes.  I want the app to open the url in a webview when the button is clicked.  Please help me out, any help is greatly appreciated.</p>
  
  <p>Can anyone please post the modified code.</p>
  
  <pre><code>activity_main.xml
  
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent" &gt;
  
  
  
  &lt;Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/textView1"
          android:layout_alignParentTop="true"
          android:text="@string/button01" 
          android:onClick="onClick"/&gt;
  &lt;Button
          android:id="@+id/button2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/textView1"
          android:layout_alignParentTop="true"
          android:text="@string/button02" 
          android:onClick="onClick"/&gt;
  
  
  &lt;/LinearLayout&gt;   
  ------------------------------
  screen3.xml
  
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent" &gt;
  
  &lt;WebView 
      android:id="@+id/webview" 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" /&gt;
  &lt;/LinearLayout&gt;
  -----------------------------------------------------
  mainactivity.java
  
  package com.example.webview;
  
  
  
  import android.support.v7.app.ActionBarActivity;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.webkit.WebSettings;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;
  
  public class MainActivity extends ActionBarActivity {
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      public void onClick(View v)
      {
          WebView myWebView = (WebView) findViewById(R.id.webview);
          myWebView.setWebViewClient(new WebViewClient());
          WebSettings webSettings = myWebView.getSettings();
          webSettings.setJavaScriptEnabled(true);
          final Context context = this;
          Intent intent = new Intent(context, SecondActivity.class);
          startActivity(intent);
         switch(v.getId())
         {
             case R.id.button1:
                 myWebView.loadUrl("http://techcrunch.com/tag/rss/");
                                  break;
  
            case R.id.button2: 
                myWebView.loadUrl("http://www.bestchance.org.au/");
                                 break;
  
            default:
                                 break;
        }
  
  
  }
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  }
  --------------------
  secondactivity.java
  package com.example.webview;
  
  import android.webkit.WebView;
  import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.webkit.WebSettings;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;
  
  public class SecondActivity extends ActionBarActivity {
  
      private WebView webView;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.screen3);
  
  
  
  
   }
  
  
  }
  -------------------------------
  </code></pre>