Android null pointer exception on ArrayList for ListView

强颜欢笑 提交于 2021-02-04 21:30:18

问题


I have a listview that shows the contents of an arraylist. I'm using a simple adaptor to make this possible like so.

public static ArrayList<String> homeScreenContacts = new ArrayList<String>();

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
                R.layout.home_screen_contacts_view, NewContact.homeScreenContacts);

The second line is giving me a null pointer exception. I thought about it and I decided it was because the arrayList is empty. So I added the following line between the arraylist declaration and the arrayadaptor declaration...

NewContact.homeScreenContacts.add("A Contact");

This solved the problem and my code worked fine but Now the list view shows "A Contact" and I dont want it to. Is there anyway to get rid of the null pointer exception problem but still have the arraylist empty? Because I want to populate it with user made contacts, not hard-coded, random strings. Thank you.

EDIT: Sorry, The arraylist is located in another class called NewContact, also, I am very beginner Android Programmer I just started.


回答1:


Simple solution just don't initialize the ListView if there is no element in the ArrayList or the ArrayList is null.

if(NewContact.homeScreenContacts != null && NewContact.homeScreenContacts.size() > 0){
   ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
            R.layout.home_screen_contacts_view, NewContact.homeScreenContacts);

   listView.setAdapter(adapter);
}

Also you need to remember that if you you haven't initialize Adapter then dont initialize the ListView and before any operation on list view you should check is it null or not.

As you have said that you want to populate when user add some contact in the application then on add event only you need to populate or update the ListAdapter.

Hope this solution will resolve your problem.




回答2:


Try this code

public class YourActivity extends Activity 

    {
        private ListView lv;
        public void onCreate(Bundle saveInstanceState) {
              setContentView(R.layout.your_layout);
             lv = (ListView) findViewById(R.id.your_list_view_id);
             // Instanciating an array list (you don't need to do this, you already have yours)
             ArrayList<String> your_array_list = new ArrayList<String>();
             your_array_list.add("foo");
             your_array_list.add("bar");
             // This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
             ArrayAdapter<String> arrayAdapter =      
             new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, your_array_list);
             lv.setAdapter(arrayAdapter); 
        }
    }



回答3:


Works fine for me:

if(arrayList.isEmpty())
{

        listView.setAdapter(null);
}
else
{
        listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
        listView.setAdapter(listAdapter);
}


来源:https://stackoverflow.com/questions/18841283/android-null-pointer-exception-on-arraylist-for-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!