title: How to add string to ListView dynamically in android?
body: <pre><code>public class MyActivity extends Activity {
      Context context;
      List&lt;String&gt; tasks;
      ArrayAdapter&lt;String&gt; adapter;
  
      /**
       * Called when the activity is first created.
       */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          context = this;
          tasks = new ArrayList&lt;String&gt;();
  
          Button add = (Button) findViewById(R.id.button);
          add.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  EditText editText = (EditText) findViewById(R.id.editText);
                  editText.setVisibility(1);
  
                  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                  imm.showSoftInput(editText, 0);
  
                  String value = editText.getText().toString();
                  tasks.add(value);
  
                  adapter = new ArrayAdapter&lt;String&gt;(context,R.id.listView,tasks);
                  ListView listView = (ListView) findViewById(R.id.listView);
                  listView.setAdapter(adapter);
              }
          });
      }
  }
  </code></pre>
  
  <p>Here, i am getting value from the user. and i am trying to add it to list view dynamically. But, it is showing an error called "Unfortunatly app is closed". it is failing in adding string value to the tasks variable. tasks is a list of string.</p>
  
  <pre><code>tasks.add(value);
  </code></pre>
  
  <p>if i try to add something else also it is failing. like,</p>
  
  <pre><code>tasks.add("something");
  </code></pre>
  
  <p>i don't know what is the problem. but i am sure that it is failing in this line, because if i remove this line, my app is working fine. if someone knows why it is failing please let me know. thanks in advance.</p>