title: app crashes on removeView
body: <p>I wrote the following application where a 3 by 4 grid of buttons displayed and user can change the grid dimensions by clicking menu items. The problem is that something strange happens with the "deleteSomething" function. Below is the complete code:</p>
  
  <pre><code>package com.example.myapplication;
  
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.view.Gravity;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.widget.Button;
  import android.widget.LinearLayout;
  
  public class MainActivity extends AppCompatActivity {
  
      LinearLayout.LayoutParams params;
      LinearLayout linearLayout;
      int _row;
      int column;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          linearLayout = new LinearLayout(this);
          linearLayout.setOrientation(LinearLayout.VERTICAL);  //Can also be done in xml by android:orientation="vertical"
          params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
          params.weight = 1.0f;
          params.gravity = Gravity.TOP;
          //layout.setBackgroundColor(0xFFFFFFFF);
          _row=3;
          column=4;
          update();
  
      }
  
  
      public void update(){
          for (int i = 0; i &lt; _row; i++) {
              LinearLayout row = new LinearLayout(this);
              row.setLayoutParams(params);
  
              for (int j = 0; j &lt; column; j++) {
                  Button btnTag = new Button(this);
                  btnTag.setLayoutParams(params);
                  btnTag.setText("Button " + (j + 1 + (i * column)));
                  btnTag.setId(j + 1 + (i * column));
                  if ((i+j) % 2 == 0) {
                      btnTag.setBackgroundColor(0xFFFF0000);
                  } else {
                      btnTag.setBackgroundColor(0x00000000);
                  }
  
                  row.addView(btnTag);
              }
              linearLayout.addView(row);
          }
  
          setContentView(linearLayout);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          menu.add(Menu.NONE, 1, Menu.NONE, "Item name");
          menu.add(Menu.NONE, 2, Menu.NONE, "Item name");
          return true;
      }
  
      public void deleteSomething(){
  
          ((LinearLayout) linearLayout.getChildAt(1)).removeViewAt(0);
          ((LinearLayout) linearLayout.getChildAt(1)).removeViewAt(1);
          ((LinearLayout) linearLayout.getChildAt(1)).removeViewAt(2);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
  
          switch (item.getItemId()) {
              case MENU_ITEM_ITEM1:
                  deleteSomething();
                  //linearLayout.removeAllViews();
                  //_row=4;
                  //column=5;
                  //update();
                  return true;
              case 2:
                  deleteSomething();
                  //linearLayout.removeAllViews();
                  _row=6;
                  column=3;
                  update();
              default:
                  return false;
          }
  
  
      }
  }
  </code></pre>
  
  <p>There is something strange about this particular part:</p>
  
  <pre><code>public void deleteSomething(){
  
              ((LinearLayout) linearLayout.getChildAt(1)).removeViewAt(0);
              ((LinearLayout) linearLayout.getChildAt(1)).removeViewAt(1);
              ((LinearLayout) linearLayout.getChildAt(1)).removeViewAt(2);
          }
  </code></pre>
  
  <p>Comment out the first two line, the third button in the second rows get deleted without a problem; comment out the second one, the first two buttons in the second get deleted without a problem either. However, when you include all of the three lines, the app crashes immediately after you click the first item in the drop down menu.</p>
  
  <p>Does anyone know why?</p>