title: Adding a ImageView from a resource to a LinearLayout in the javacode
body: <p>I am new to programming Android but have some experience with Java, albeit still pretty inexperienced. </p>
  
  <p>I'll firstly post the XML document so it's easier to describe.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      tools:context=".MainActivity" &gt;
  
      &lt;LinearLayout 
          android:layout_height="match_parent"
          android:layout_width="match_parent"
          android:orientation="horizontal"
          android:layout_weight="2"
          android:background="#4E2FEB"&gt;
      &lt;/LinearLayout&gt;
  
  
      &lt;HorizontalScrollView 
          android:layout_height="wrap_content"
          android:layout_width="match_parent"
          android:background="#E01F39" &gt;
  
          &lt;LinearLayout android:id="@+id/card_tray"
              android:layout_height="wrap_content"
              android:layout_width="match_parent"
              android:orientation="horizontal" &gt;
  
          &lt;/LinearLayout&gt;
      &lt;/HorizontalScrollView&gt;
   &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>I want to add ImageView's to the HorizontalScrollView in java code. Here is the code i got so far.</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState); 
          setContentView(R.layout.activity_main);
  
          LinearLayout l = (LinearLayout)findViewById(R.id.card_tray);
  
          l.addView((ImageView)findViewById(R.id.xo_1));
          l.addView((ImageView)findViewById(R.id.xo_2));
  }
  </code></pre>
  
  <p>Here is the other .xml file i have that contains the ImageView views i'm trying to refer to.</p>
  
  <pre><code>&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;ImageView  android:id="@+id/xo_1"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:adjustViewBounds="true"
                  android:paddingRight="10dp" 
                  android:src="@drawable/xo_1" /&gt; 
  
      &lt;ImageView  android:id="@+id/xo_2"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:adjustViewBounds="true"
                  android:paddingRight="10dp" 
                  android:src="@drawable/xo_2" /&gt;  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>When i try to run it on my Tablet using Eclipse it crashes with the usual 'App is not responding' message.</p>
  
  <p><strong>EDIT:</strong>
  Here is the LogCat output.</p>
  
  <blockquote>
    <p>03-30 01:38:13.053: D/AndroidRuntime(29659): Shutting down VM 03-30
    01:38:13.053: W/dalvikvm(29659): threadid=1: thread exiting with
    uncaught exception (group=0x41f0b2a0) 03-30 01:38:13.053:
    E/AndroidRuntime(29659): FATAL EXCEPTION: main 03-30 01:38:13.053:
    E/AndroidRuntime(29659): java.lang.RuntimeException: Unable to start
    activity ComponentInfo{kpapps.testapp/kpapps.testapp.MainActivity}:
    java.lang.NullPointerException 03-30 01:38:13.053:
    E/AndroidRuntime(29659):  at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
    03-30 01:38:13.053: E/AndroidRuntime(29659):  at
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
    03-30 01:38:13.053: E/AndroidRuntime(29659):  at
    android.app.ActivityThread.access$700(ActivityThread.java:140) 03-30
    01:38:13.053: E/AndroidRuntime(29659):    at
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
    03-30 01:38:13.053: E/AndroidRuntime(29659):  at
    android.os.Handler.dispatchMessage(Handler.java:99) 03-30
    01:38:13.053: E/AndroidRuntime(29659):    at
    android.os.Looper.loop(Looper.java:137) 03-30 01:38:13.053:
    E/AndroidRuntime(29659):  at
    android.app.ActivityThread.main(ActivityThread.java:4921) 03-30
    01:38:13.053: E/AndroidRuntime(29659):    at
    java.lang.reflect.Method.invokeNative(Native Method) 03-30
    01:38:13.053: E/AndroidRuntime(29659):    at
    java.lang.reflect.Method.invoke(Method.java:511) 03-30 01:38:13.053:
    E/AndroidRuntime(29659):  at
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
    03-30 01:38:13.053: E/AndroidRuntime(29659):  at
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 03-30
    01:38:13.053: E/AndroidRuntime(29659):    at
    dalvik.system.NativeStart.main(Native Method) 03-30 01:38:13.053:
    E/AndroidRuntime(29659): Caused by: java.lang.NullPointerException
    03-30 01:38:13.053: E/AndroidRuntime(29659):  at
    android.view.ViewGroup.addView(ViewGroup.java:3481) 03-30
    01:38:13.053: E/AndroidRuntime(29659):    at
    android.view.ViewGroup.addView(ViewGroup.java:3464) 03-30
    01:38:13.053: E/AndroidRuntime(29659):    at
    kpapps.testapp.MainActivity.onCreate(MainActivity.java:22) 03-30
    01:38:13.053: E/AndroidRuntime(29659):    at
    android.app.Activity.performCreate(Activity.java:5188) 03-30
    01:38:13.053: E/AndroidRuntime(29659):    at
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
    03-30 01:38:13.053: E/AndroidRuntime(29659):  at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
    03-30 01:38:13.053: E/AndroidRuntime(29659):  ... 11 more</p>
  </blockquote>
  
  <p><strong>EDIT:</strong> As per requested. Line 22 of MainActivity.java</p>
  
  <pre><code>l.addView((ImageView)findViewById(R.id.xo_1));
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <h2>Revision</h2>
  
  <p>I changed the java code to;</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState); 
          setContentView(R.layout.activity_main);
  
          LinearLayout l = (LinearLayout)findViewById(R.id.card_tray);
  
          View cards = getLayoutInflater().inflate(R.layout.skill_cards, null);
  
          ImageView imageView = (ImageView)cards.findViewById(R.id.xo_1);
          ImageView imageView2 = (ImageView)cards.findViewById(R.id.xo_2);
  
          l.addView(imageView);
          l.addView(imageView2);
  }
  </code></pre>
  
  <p>But it still crashes just with a different error. Here is the new LogCat;</p>
  
  <blockquote>
    <p>03-30 02:10:31.833: E/AndroidRuntime(4978): FATAL EXCEPTION: main
    03-30 02:10:31.833: E/AndroidRuntime(4978):
    java.lang.RuntimeException: Unable to start activity
    ComponentInfo{kpapps.testapp/kpapps.testapp.MainActivity}:
    java.lang.IllegalStateException: The specified child already has a
    parent. You must call removeView() on the child's parent first. 03-30
    02:10:31.833: E/AndroidRuntime(4978):     at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
    03-30 02:10:31.833: E/AndroidRuntime(4978):   at
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
    03-30 02:10:31.833: E/AndroidRuntime(4978):   at
    android.app.ActivityThread.access$700(ActivityThread.java:140) 03-30
    02:10:31.833: E/AndroidRuntime(4978):     at
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
    03-30 02:10:31.833: E/AndroidRuntime(4978):   at
    android.os.Handler.dispatchMessage(Handler.java:99) 03-30
    02:10:31.833: E/AndroidRuntime(4978):     at
    android.os.Looper.loop(Looper.java:137) 03-30 02:10:31.833:
    E/AndroidRuntime(4978):   at
    android.app.ActivityThread.main(ActivityThread.java:4921) 03-30
    02:10:31.833: E/AndroidRuntime(4978):     at
    java.lang.reflect.Method.invokeNative(Native Method) 03-30
    02:10:31.833: E/AndroidRuntime(4978):     at
    java.lang.reflect.Method.invoke(Method.java:511) 03-30 02:10:31.833:
    E/AndroidRuntime(4978):   at
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
    03-30 02:10:31.833: E/AndroidRuntime(4978):   at
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 03-30
    02:10:31.833: E/AndroidRuntime(4978):     at
    dalvik.system.NativeStart.main(Native Method) 03-30 02:10:31.833:
    E/AndroidRuntime(4978): Caused by: java.lang.IllegalStateException:
    The specified child already has a parent. You must call removeView()
    on the child's parent first. 03-30 02:10:31.833:
    E/AndroidRuntime(4978):   at
    android.view.ViewGroup.addViewInner(ViewGroup.java:3672) 03-30
    02:10:31.833: E/AndroidRuntime(4978):     at
    android.view.ViewGroup.addView(ViewGroup.java:3543) 03-30
    02:10:31.833: E/AndroidRuntime(4978):     at
    android.view.ViewGroup.addView(ViewGroup.java:3488) 03-30
    02:10:31.833: E/AndroidRuntime(4978):     at
    android.view.ViewGroup.addView(ViewGroup.java:3464) 03-30
    02:10:31.833: E/AndroidRuntime(4978):     at
    kpapps.testapp.MainActivity.onCreate(MainActivity.java:24) 03-30
    02:10:31.833: E/AndroidRuntime(4978):     at
    android.app.Activity.performCreate(Activity.java:5188) 03-30
    02:10:31.833: E/AndroidRuntime(4978):     at
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
    03-30 02:10:31.833: E/AndroidRuntime(4978):   at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
    03-30 02:10:31.833: E/AndroidRuntime(4978):   ... 11 more</p>
  </blockquote>