title: Android programmatically add ImageView
body: <p>I just started learning Android programming.
  I created a new project (API 19) with a blank activity and tried to add an ImageView like this:
  (in the MainActivity's onCreate() )</p>
  
  <pre><code>setContentView(R.layout.activity_main);
  View view = (View) findViewById(R.layout.activity_main); 
  LinearLayout picLL = new LinearLayout(MainActivity.this);
  picLL.layout(0, 0, 100, 100);
  picLL.setLayoutParams(new LayoutParams(100, 100));
  picLL.setOrientation(LinearLayout.HORIZONTAL);
  ((ViewGroup) view).addView(picLL); // &lt;- This is line 36
  ImageView myImage = new ImageView(this);
  myImage.setImageResource(R.drawable.ic_launcher);
  picLL.addView(myImage);
  </code></pre>
  
  <p>However the app crashes immediately after launch.
  I've tried numerous other answers from Stackoverflow, but none worked. There seems to be something I am missing...</p>
  
  <p>This is the LogCat output:</p>
  
  <pre><code>04-17 10:02:35.611: E/AndroidRuntime(1402): FATAL EXCEPTION: main
  04-17 10:02:35.611: E/AndroidRuntime(1402): Process: com.example.test, PID: 1402
  04-17 10:02:35.611: E/AndroidRuntime(1402): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException
  04-17 10:02:35.611: E/AndroidRuntime(1402):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
  04-17 10:02:35.611: E/AndroidRuntime(1402):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
  04-17 10:02:35.611: E/AndroidRuntime(1402):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
  04-17 10:02:35.611: E/AndroidRuntime(1402):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  04-17 10:02:35.611: E/AndroidRuntime(1402):     at android.os.Handler.dispatchMessage(Handler.java:102)
  04-17 10:02:35.611: E/AndroidRuntime(1402):     at android.os.Looper.loop(Looper.java:136)
  04-17 10:02:35.611: E/AndroidRuntime(1402):     at android.app.ActivityThread.main(ActivityThread.java:5017)
  04-17 10:02:35.611: E/AndroidRuntime(1402):     at java.lang.reflect.Method.invokeNative(Native Method)
  04-17 10:02:35.611: E/AndroidRuntime(1402):     at java.lang.reflect.Method.invoke(Method.java:515)
  04-17 10:02:35.611: E/AndroidRuntime(1402):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  04-17 10:02:35.611: E/AndroidRuntime(1402):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  04-17 10:02:35.611: E/AndroidRuntime(1402):     at dalvik.system.NativeStart.main(Native Method)
  04-17 10:02:35.611: E/AndroidRuntime(1402): Caused by: java.lang.NullPointerException
  04-17 10:02:35.611: E/AndroidRuntime(1402):     at com.example.test.MainActivity.onCreate(MainActivity.java:36)
  04-17 10:02:35.611: E/AndroidRuntime(1402):     at android.app.Activity.performCreate(Activity.java:5231)
  04-17 10:02:35.611: E/AndroidRuntime(1402):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  04-17 10:02:35.611: E/AndroidRuntime(1402):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
  04-17 10:02:35.611: E/AndroidRuntime(1402):     ... 11 more
  04-17 10:02:38.771: I/Process(1402): Sending signal. PID: 1402 SIG: 9
  </code></pre>