title: How can I implement webviews in different layouts without having to make a class for every layout?
body: <p>okey, so i have a main class with 8 buttons, and i have each button to setcontentview layout 1, 2, etc.. I have implemented webviews with different IDs in each of the layouts, but i can seem to get it to work. The plan is to show a picture from a website in each layout, and since im not great at programming, i thought i'd try this first. Here's my questions: How can i make this setup work (in terms that i understand), and what's the most optimal way to get this result? I get nullpointererror.</p>
  
  <pre><code>import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.webkit.WebView;
  import android.widget.Button;
  import android.widget.Toast;
  
  
  
  public class Main extends Activity implements View.OnClickListener {
  
  
   Button btn1, btn2, //etc..;
   WebView webview_1, webview_2, //etc..;
  
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main_layout);
  
  
      Button btn1 = (Button)findViewById(R.id.button1);
      btn1.setOnClickListener(this);
      Button btn2 = (Button)findViewById(R.id.button2);
      btn2.setOnClickListener(this);
      //and so on
  
      WebView one = (WebView) findViewById(R.id.webview1);
      one.getSettings().setJavaScriptEnabled(true);
      one.loadUrl("www.example.com/jpg);
  
      WebView two = (WebView) findViewById(R.id.webview2);
      two.getSettings().setJavaScriptEnabled(true);
      two.loadUrl("www.example.com/jpg);
  
   //etc
   }
  
  public void onClick(View v) {
  switch (v.getId()) {
  case R.id.button1:
      setContentView(R.layout.layout1);
      return;
  case R.id.button2:
      setContentView(R.layout.layout2);
      return;
  //etc
  
  }
  
  //main_layout
  
   &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:paddingLeft="16dp"
  android:paddingRight="16dp" &gt;
  
  &lt;LinearLayout
      android:layout_width="450dip"
      android:layout_height="450dip"
  
      android:layout_alignParentRight="true"
      android:layout_centerVertical="true"
      android:background="@drawable/norge"
      android:orientation="vertical"
      android:padding="150dp" &gt;
  
  &lt;/LinearLayout&gt;
  
  &lt;TextView
      android:id="@+id/textView1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="7dp"
      android:gravity="center"
      android:text="@string/overskrift_velg_sensor"
      android:textSize="25dp"
      tools:context=".Main" /&gt;
  
  &lt;Button
      android:id="@+id/button1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
  
  
      android:autoLink="web"
      android:clickable="true"
      android:linksClickable="true"
      android:text="@string/button1" /&gt;
  
  &lt;Button
      android:id="@+id/button2"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
  
      android:autoLink="web"
      android:clickable="true"
      android:linksClickable="true"
      android:text="@string/button2" /&gt;
  &lt;/LinearLayout&gt;
  
  //layout1
  
   &lt;?xml version="1.0" encoding="utf-8"?&gt;
   &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" &gt;
  
  
  &lt;TextView
      android:id="@+id/textView1"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:text="Object"
      android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
  
  &lt;WebView
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:id="@+id/webview1"
   android:layout_weight="4"&gt;
  
   &lt;/WebView&gt;
  
   &lt;/LinearLayout&gt;
  
   //layout2
  
   &lt;?xml version="1.0" encoding="utf-8"?&gt;
   &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" &gt;
  
  
  &lt;TextView
      android:id="@+id/textView2"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:text="Object2"
      android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
  
   &lt;WebView
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:id="@+id/webview2"
   android:layout_weight="4"&gt;
  
   &lt;/WebView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>