1

Im new to programming with Android and i was trying to make a ListView by allowing a user to input a text through EditText. i finally got it working however i am not quite sure why my first approach did not work:

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,values);
lv.setAdapter(adapter);

This caused a force close when the app was launched, and i found out it was due to lv.setAdapter(adapter)

After going through many stackoverflow questions, i saw one answer that stated there was no need to use ArrayList and ArrayAdapter. so i tried the following and it worked:

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    lv.setAdapter(adapter);

This time it worked perfectly when i didnt create the Array List.

So, my question is why does this work when i didnt define/create a String Array in my ArrayAdapter, and why my first method didnt work.

thanks for the help in advance, and im sorry if i didnt post enough code.

EDIT: HERE IS MY FIRST METHOD BELOW i tried to recreate my first method and it is below: it still force closes like the first time. And just to take note, in my second method(which works) all i did was not use ArrayList and take the 3rd argument in the arrayAdapter construction. Also now that your answer have told me that i probably had values as "null", i think you are correct. How could i correct this for use in the future? Thanks!

public class TaskPage extends SherlockActivity {

EditText display;
ListView lv;
ArrayAdapter<String> adapter;
Button addButton;
ArrayList<String> values;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    display = (EditText) findViewById(R.id.editText1);
    lv = (ListView) findViewById(R.id.listView1);
    addButton = (Button) findViewById(R.id.button1);


    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
    lv.setAdapter(adapter);


    addButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String task = display.getText().toString();
            values.add(task);
            adapter.notifyDataSetChanged();

        }
    });
8
  • Do you have the stack trace from when the force close happened? Commented Jan 4, 2013 at 18:21
  • values was the name of my ArrayList. i had a Button recieve the EditText text and then convert to a String. and then i put this into the arraylist known as values Commented Jan 4, 2013 at 18:27
  • Does the assigned value occupy the whole of the ArrayList? If your ArrayList is bigger than what you have used it for, then there are unused positions in the array with null values, which could cause the force-close. The answer by Sam suggests the same. Commented Jan 4, 2013 at 18:29
  • ah i think i get what you mean. thanks! Commented Jan 4, 2013 at 18:34
  • 1
    "But can soemone tell me as to why i dont need to have a String Array argument in my ArrayAdapter constructor" This construtor assumes that you will call add(), insert(), etc later to populate your ListView. Commented Jan 4, 2013 at 18:37

3 Answers 3

4

If the second approach worked but the first one didn't then values has at least one element that is null. This will throw an exception when the Adapter tries to bind this null value with a TextView.
Simply remove any null data from values.

From ArrayAdapter's source code:

T item = getItem(position);
if (item instanceof CharSequence) { // A null value throws an NPE here
    text.setText((CharSequence)item);
} else {
    text.setText(item.toString());
}

The more I think about it values itself could be null too... This is where the LogCat errors and debugger are very handy.

Sign up to request clarification or add additional context in comments.

3 Comments

im also sure values is null
I didnt try to have values be null however i may have done it. I dont have the full code of my first method so i cant fully explain.
"I didnt try to have values be null" Well, no one intentionally crashes their app, but we all do it. :) "I dont have the full code of my first method so i cant fully explain." Has my answer helped you understand is happening? If so please accept it because I'm not sure how anyone can answer your original question beyond what I have already written without more details...
0

After making sure that values and lv is not null, and it does not contain any null elements, This should work in an YourActivity, or a child class belongs to it. :

adapter = new ArrayAdapter<String>(YourActivity.this, android.R.layout.simple_list_item_1, values);
lv.setAdapter(adapter);

5 Comments

If the second approach worked with this how does change the Context reference solve the error?
I actually did try this method(changing the Context Reference)..but it still caused a force close. So i think @Sam was correct in that i had the Array List, values, as null.
@Sam: It just occured to me that there might be a context-related problem; just if we ruled out that our ListView and ArrayList object is not null. Referencing the context won't cause problems, it just may solve the error. But if we make sure that values and lv are OK, then this code should work, in my humble opinion.
@user1949400 This is just a long-shot, but i just wanted to point it out.
ok i will post the code when i try this again. Because i dont want to get any more problems due to this in the future as i add more features.
0

This post is very old, but I came across it and wanted to add what the fix was for me. According to the documentation (http://developer.android.com/reference/android/widget/ArrayAdapter.html), "By default this class expects that the provided resource id references a single TextView." I was using a RelativeLayout with a TextView inside it. This is kind-of obvious, but in case it helps someone else...

Comments

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.