title: Android LinearLayout.removeViewAt (int i) throws exceptions?
body: <p>I have the following code: </p>
  
  <pre><code>for (int i = 0; i &lt; linearLayout.getChildCount() - 5; i++) //IN this code, please assume the child count is 10;
  {
      View v = linearLayout.getChildAt(i);
      Standard.Loge("REMOVING: " + i + " " + (v == null));
      linearLayout.removeViewAt(i);
  }
  </code></pre>
  
  <p>This outputs the following:</p>
  
  <pre><code>REMOVING: 0 false
  REMOVING: 1 false
  REMOVING: 2 true
  </code></pre>
  
  <p>the app crashes, even though the views at index 2 - 4 have not been removed, throwing this error:</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.unFocus(android.view.View)' on a null object reference
      at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4937)
      at android.view.ViewGroup.removeViewAt(ViewGroup.java:4899)
      at (((The last line in the for loop above)))
  </code></pre>
  
  <p>It would appear that the views are null, even though getChildCount registers that the views exist, and my guess is that this is causing removeChildAt to crash. I am adding the views dynamically in java so I am not able to use findViewByID. I am really at a loss here. How can I fix this crash so I can remove these views?</p>