1. Download our Official Android App: Forums for Android!

Apps Adapter/ListView IndexOutOfBounds

Discussion in 'Android Development' started by CeZet, Jun 5, 2011.

  1. CeZet

    CeZet Lurker
    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    3
    Joined:
    Jun 5, 2011

    Jun 5, 2011
    3
    0
    25
    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 (Text):
    1.  public void onCreate(Bundle savedInstanceState) {
    2.         super.onCreate(savedInstanceState);
    3.         setContentView(R.layout.menu);
    4.         ListView menuList = (ListView) findViewById(R.id.listViewMenuItems);
    5.         String[] items = { getResources().getString(R.string.locate_menu_item) ,
    6.                 getResources().getString(R.string.settings_menu_item),
    7.                 getResources().getString(R.string.help_menu_item)};
    8.         ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu_item, items);
    9.         menuList.setAdapter(adapt);    
    10.     }
    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.

    [​IMG]

    Can anyone help and say what am I doing wrong? I`m really clueless now :/
     

    Advertisement

  2. CeZet

    CeZet Lurker
    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    3
    Joined:
    Jun 5, 2011

    Jun 5, 2011
    3
    0
    25
    Poland
    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 (Text):
    1. ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu_item, items);
    2.         menuList.setAdapter(adapt);
    3.         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.
     

Share This Page

Loading...