title: android: crash when insert entry to listview using a private method
body: <p>I create following codes to insert items to a listview. I will have a button to call the updateDisplay() method to insert the item and update the view.
  I add also the onclick of the listview to provide a check of the insert.
  However, the app crash when calling the updateDisplay(). The onclick works. What is wrong?</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main); // added back
      ListView listView = (ListView) findViewById(R.id.listView);
      String[] values = new String[] { "Android List View",
              "Adapter implementation", "Simple List View In Android",
              "Create List View Android", "Android Example",
              "List View Source Code", "List View Array Adapter",
              "Android Example List View" };
  
      final ArrayList&lt;String&gt; listItems = new ArrayList&lt;String&gt;();
      for (int i = 0; i &lt; values.length; ++i) {
          listItems.add(values[i]);
      }
  
      final ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this,
              android.R.layout.simple_list_item_1, android.R.id.text1,
              listItems);
  
      // Assign adapter to ListView
      listView.setAdapter(adapter);
  
      listView.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                  int position, long id) {
  
              final String item = (String) parent.getItemAtPosition(position);
              listItems.add(0, "Clicked : " + item);
  
              adapter.notifyDataSetChanged();
  
          }
  
      });
  }
  
  //
  private void updateDisplay() {
  
      listItems.add(0, "Clicked : " + "my data");
      adapter.notifyDataSetChanged();
  
  } 
  </code></pre>
  
  <p>code after debugging:</p>
  
  <pre><code>public class ButtonActivity extends Activity {
  int count = 0;
  ArrayList&lt;String&gt; listItems;
  ArrayAdapter&lt;String&gt; adapter;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      values = new String[] { "Android List View", "Adapter implementation", "Simple List View In Android", "Create List View Android", "Android Example", "List View Source Code", "List View Array Adapter", "Android Example List View" }; listItems = new ArrayList&lt;String&gt;(); for (int i = 0; i &lt; values.length; ++i) { listItems.add(values[i]); } adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, android.R.id.text1, listItems); // Assign adapter to ListView listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) { final String item = (String) parent.getItemAtPosition(position); listItems.add(0, "Clicked : " + item); adapter.notifyDataSetChanged(); } }); } // private void updateDisplay() { listItems.add(0, "Clicked : " + "my data"); adapter.notifyDataSetChanged(); }
      ListView listView = (ListView) findViewById(R.id.listView);
      String[] values = new String[] { "Android List View",
              "Adapter implementation", "Simple List View In Android",
              "Create List View Android", "Android Example",
              "List View Source Code", "List View Array Adapter",
              "Android Example List View" };
  
      listItems = new ArrayList&lt;String&gt;();
      for (int i = 0; i &lt; values.length; ++i) {
          listItems.add(values[i]);
      }
  
     adapter = new ArrayAdapter&lt;String&gt;(this,
              android.R.layout.simple_list_item_1, android.R.id.text1,
              listItems);
  
      // Assign adapter to ListView
      listView.setAdapter(adapter);
  
      listView.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                  int position, long id) {
  
              final String item = (String) parent.getItemAtPosition(position);
              listItems.add(0, "Clicked : " + item);
  
              adapter.notifyDataSetChanged();
  
          }
  
      });
  
      final Button button = (Button) findViewById(R.id.button);
  
      button.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              button.setText("Got Pressed:" + ++count);
              updateDisplay();
  
  
          }
      });
  }
  private void updateDisplay() {
  
      listItems.add(0, "Clicked : " + "my data");
      adapter.notifyDataSetChanged();
  } 
  }
  </code></pre>