title: Android: start listener across classes
body: <p>Hey I am currently working myself my way into custom views on Android but I now run into a problem setting a listener. If I call the listener from within the custom view class, everything works, but I want to set the listener from my MainActivity which is where I am running into problems right now. Every time, I call the Listener from my MainActivity I just get a nullpointer, no matter what I pass to the method. Here the code of the custom class</p>
  
  <pre><code>public class RoundKnobButton extends RelativeLayout implements OnGestureListener {
  
  public interface RoundKnobButtonListener {
      public void onStateChange(boolean newstate) ;
      public void onRotate(int percentage);
  }
  
  
  public RoundKnobButtonListener m_listener;
  
  public void SetListener(RoundKnobButtonListener l) {
      m_listener = l;
  } 
  </code></pre>
  
  <p>and if I call the Listener from within this class like</p>
  
  <pre><code>     SetListener(new RoundKnobButtonListener() {
              public void onStateChange(boolean newstate) {
  
              }
  
              public void onRotate(final int percentage) {
              System.out.println(percentage); 
       }
                  }); 
  </code></pre>
  
  <p>It returns the value perfectly. Now, when I want to do something like</p>
  
  <pre><code>rv=(RoundKnobButton) findViewById(R.id.jogView);
  rv.SetListener(new RoundKnobButton.RoundKnobButtonListener() {
  public void onStateChange(boolean newstate) {
  }
  
  public void onRotate(final int percentage) {
      System.out.println(percentage); 
          }
      }); 
  </code></pre>
  
  <p>in my MainActivity, I get </p>
  
  <pre><code>02-10 14:31:45.354: W/dalvikvm(19006): threadid=1: thread exiting with uncaught exception (group=0x418259a8)
  02-10 14:31:45.360: E/AndroidRuntime(19006): FATAL EXCEPTION: main
  02-10 14:31:45.360: E/AndroidRuntime(19006): java.lang.NullPointerException
  02-10 14:31:45.360: E/AndroidRuntime(19006):    at  .buildUI( .java:191)
  02-10 14:31:45.360: E/AndroidRuntime(19006):    at  .access$6( .java:125)
  02-10 14:31:45.360: E/AndroidRuntime(19006):    at  .onPostExecute( .java:334)
  </code></pre>
  
  <p>Where line 191 refers to <code>rv.SetListener(new RoundKnobButton.RoundKnobButtonListener()</code> </p>
  
  <p>I already tried passing all kinds of parameters with all kinds of casts, but nothing seems to help here.</p>
  
  <p>Thanks in advance, Alex</p>