0
public class MyActivity extends Activity {
    Context context;
    List<String> tasks;
    ArrayAdapter<String> 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<String>();

        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<String>(context,R.id.listView,tasks);
                ListView listView = (ListView) findViewById(R.id.listView);
                listView.setAdapter(adapter);
            }
        });
    }
}

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.

tasks.add(value);

if i try to add something else also it is failing. like,

tasks.add("something");

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.

6
  • can you post the error? I'd guess it's an NPE on that tasks list, maybe after rotation? Commented Sep 24, 2013 at 16:10
  • its a runtime error. how can i post? Commented Sep 24, 2013 at 16:11
  • what does your logcat say? Commented Sep 24, 2013 at 16:12
  • take a look at how to use logcat for your development environment (intelliJ, eclipse, android studio) just a quick google search will get you what you want, you'll be able to see where the problem is and why it's happening. Commented Sep 24, 2013 at 16:13
  • @HemanthGowda try this adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item1,tasks) also there is not need to set the adapter to listview everytime. just call adapter.notifydataSetChanged to refresh listview Commented Sep 24, 2013 at 16:20

1 Answer 1

5

There are too many wrong in your source code. Try the following code and, understand what you are writing rather than copy pasting blindly.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    context = this;
    tasks = new ArrayList<String>();

    // instances all your variables on initial only
    Button add = (Button) findViewById(R.id.button);
    final EditText editText = (EditText) findViewById(R.id.editText);

    // second parameter is row layout, 
    adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item1,tasks);
    ListView listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(adapter);



    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            editText.setVisibility(1);

            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(editText, 0);

            String value = editText.getText().toString();
            tasks.add(value);

            // this method will refresh your listview manually 
            adapter.notifyDataSetChanged();
        }
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

the resource hast a little error :-) android.R.layout.simple_list_item_1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.