• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Adapter/ListView IndexOutOfBounds

CeZet

Lurker
Jun 5, 2011
3
0
Poland
Hi,

I`ve encountered a really strange problem. Propably some of you already saw that piece of code before (it`s out of a popular book):

Code:
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
        ListView menuList = (ListView) findViewById(R.id.listViewMenuItems);
        String[] items = { getResources().getString(R.string.locate_menu_item) ,
                getResources().getString(R.string.settings_menu_item),
                getResources().getString(R.string.help_menu_item)}; 
        ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu_item, items);
        menuList.setAdapter(adapt);    
    }

So what`s the problem you ask? Well, the debugger acts really strange and return an IndexOutOfBounds error on the ListView. The lines are also really weird and it looks like an inside error. Displayed below.

outofbounds.jpg


Can anyone help and say what am I doing wrong? I`m really clueless now :/
 
Sorry for double post, but I found out what is the most propable reason: after setting the adapter for a list, the list still has no children! That means that this debug code:


Code:
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu_item, items);
        menuList.setAdapter(adapt);
        Log.d(TAG, Integer.toString(menuList.getChildCount()));


Returns 0 in debug log. Now I`m really confused. No wonder that ListView.getChildenAt(id) returns null.

Is there a way to make sure that android doesn`t call the ListView.getChildenAt(id) method before ListView has children? I get usually two of these errors and after I resume my pogram everything is fine.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones