title: Android: Create ImageView outside MainActivity
body: <p>I'm pretty new to android programming. And now, I'm trying to create a ImageView programmatically. But not in the MainActivity, but in a second class I created.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>package com.example.joystick;
  
  import android.app.Activity;
  import android.widget.ImageView;
  import android.widget.RelativeLayout;
  import android.widget.RelativeLayout.LayoutParams;
  
  public class Joystick extends Activity{
  
      ImageView imView;
      RelativeLayout Layout;
  
  
  
  public void create(int x, int y, RelativeLayout layout) {
      Layout = layout;
      imView = new ImageView(this);
      imView.setImageResource(R.drawable.joystickknuppel);
      LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
      params.setMargins(x, y, 0, 0);
      imView.setLayoutParams(params);
  
      Layout.addView(imView);
      }
  
  }
  </code></pre>
  
  <p>If I load the app to my device, it crashes. But if I copy the "create" method into MainActivity, it works well.</p>
  
  <p>Can someone tell me why this happens, and how to fix it?</p>
  
  <p>Thanks a lot!</p>