title: Clickable TableRow on Android
body: <p>I have a simple program i have made in java for android but am having difficulty in making the tablerow clickable. When the user clicks a row i wish for a certain picture to be displayed in the imageview below. Here is my xml code:</p>
  
  <pre><code>     &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="#FF909090"
      android:stretchColumns="1" &gt;
  
      &lt;TableRow&gt;
  
          &lt;TextView
              android:layout_margin="2dip"
              android:layout_marginRight="2dip"
              android:background="#0000ff"
              android:padding="3dip"
              android:text="I" /&gt;
  
          &lt;TextView
              android:layout_margin="2dip"
              android:layout_marginLeft="1dip"
              android:background="#0000ff"
              android:gravity="center"
              android:padding="3dip"
              android:text="A"
              android:onClick="Achord" /&gt;
      &lt;/TableRow&gt;
  
      &lt;TableRow&gt;
  
          &lt;TextView
              android:layout_column="0"
              android:layout_margin="2dip"
              android:background="#0000ff"
              android:padding="3dip"
              android:text="II" /&gt;
  
          &lt;TextView
              android:layout_margin="2dip"
              android:background="#0000ff"
              android:gravity="center"
              android:padding="3dip"
              android:text="Bm" /&gt;
      &lt;/TableRow&gt;
  
      &lt;TableRow&gt;
  
          &lt;TextView
              android:layout_column="0"
              android:layout_margin="2dip"
              android:background="#0000ff"
              android:padding="3dip"
              android:text="III" /&gt;
  
          &lt;TextView
              android:layout_margin="2dip"
              android:background="#0000ff"
              android:gravity="center"
              android:padding="3dip"
              android:text="C#m" /&gt;
      &lt;/TableRow&gt;
  
      &lt;View
          android:layout_height="2dip"
          android:background="#FF909090" /&gt;
  
      &lt;TableRow android:background="#0000ff" &gt;
      &lt;/TableRow&gt;
  
      &lt;TableRow&gt;
  
          &lt;TextView
              android:layout_margin="2dip"
              android:background="#0000ff"
              android:padding="3dip"
              android:text="IV" /&gt;
  
          &lt;TextView
              android:layout_margin="2dip"
              android:background="#0000ff"
              android:gravity="center"
              android:padding="3dip"
              android:text="D" /&gt;
      &lt;/TableRow&gt;
  
      &lt;View
          android:layout_height="2dip"
          android:background="#FF909090" /&gt;
  
      &lt;TableRow
          android:id="@+id/tableRow1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" &gt;
      &lt;/TableRow&gt;
  
      &lt;TableRow&gt;
  
          &lt;TextView
              android:layout_margin="2dip"
              android:layout_marginRight="2dip"
              android:background="#0000ff"
              android:padding="3dip"
              android:text="V" /&gt;
  
          &lt;TextView
              android:layout_margin="2dip"
              android:layout_marginLeft="1dip"
              android:background="#0000ff"
              android:gravity="center"
              android:padding="3dip"
              android:text="E" /&gt;
      &lt;/TableRow&gt;
  
      &lt;TableRow&gt;
  
          &lt;TextView
              android:layout_margin="2dip"
              android:layout_marginRight="2dip"
              android:background="#0000ff"
              android:padding="3dip"
              android:text="VI" /&gt;
  
          &lt;TextView
              android:layout_margin="2dip"
              android:layout_marginLeft="1dip"
              android:background="#0000ff"
              android:gravity="center"
              android:padding="3dip"
              android:text="F#m" /&gt;
      &lt;/TableRow&gt;
  
      &lt;TableRow&gt;
  
          &lt;TextView
              android:layout_margin="2dip"
              android:layout_marginRight="2dip"
              android:background="#0000ff"
              android:padding="3dip"
              android:text="VII" /&gt;
  
          &lt;TextView
              android:layout_margin="2dip"
              android:layout_marginLeft="1dip"
              android:background="#0000ff"
              android:gravity="center"
              android:padding="3dip"
              android:text="G#dim" /&gt;
      &lt;/TableRow&gt;
  
  
  
  
      &lt;TextView
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="0.05"
          android:background="#000000"
          android:maxLines="10"
          android:text="The relative minor of the Major key of A is F#m. The 12 bar blues progression in A is: \n A | A | A | A | D | D | A | A | E | D | A | E" /&gt;
  
  
      &lt;ImageView
          android:id="@+id/imageView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="0.03"
          android:src="@drawable/android_focused" /&gt;
  </code></pre>
  
  <p>and here is my java code:</p>
  
  <pre><code>package com.coreservlets.widgets;
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.TableRow;
  import com.welly.keychords.R;
  
  
  
  
  
   public class keya extends Activity {
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.keya);
  }
  }
  </code></pre>
  
  <p>what i want to happen is that imageview1 will display a different image depending on what row is clicked. Any help would be greatly appreciated</p>
  
  <p>As sam suggested i have ammended my code to this:</p>
  
  <pre><code>public class keya extends Activity {
  
      ImageView imagev = (ImageView) findViewById(R.id.imageView1);
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.keya);
      }
  
      public void rowClick(View view) {
          switch(view.getId()) {
          case R.id.one:
              // Load image from Drawable folder
              imagev.setImageResource(R.drawable.android_normal); // example file for now
              break;
          }
      }
     }
  </code></pre>
  
  <p>but now i have a null pointer exception on running that class. all compiles fine though</p>