Skip to content
This repository has been archived by the owner on Apr 30, 2020. It is now read-only.

java.lang.IndexOutOfBoundsException when selecting section from child #214

Closed
vangliana opened this issue Mar 5, 2015 · 10 comments
Closed

Comments

@vangliana
Copy link

Hi Folks,

I get the following exception when changing to another section from a child section:

java.lang.IndexOutOfBoundsException
            at java.util.LinkedList.remove(LinkedList.java:660)
            at it.neokree.materialnavigationdrawer.MaterialNavigationDrawer.setFragment(MaterialNavigationDrawer.java:968)

My code to change to a section is as following:

public void selectSection(final Class<? extends BaseFragment> targetFragmentClazz) {
        final MaterialSection currentSection = getCurrentSection();
        final MaterialSection targetSection = getSection(targetFragmentClazz);
        currentSection.unSelect();
        setFragment(targetSection.getTargetFragment(), targetSection.getTitle());
        setSection(targetSection);
        targetSection.select();
    }

Edit:

After doing some investigation, it seems to me that the problem is here in this line ' else for(int i = childFragmentStack.size()-1;i >= 0;i++)' in the following method:

/**
     * Set the fragment to the activity content.<br />
     * N.B. If you want to support the master/child flow, please consider to use setFragmentChild instead
     *
     * @param fragment to replace into the main content
     * @param title to set into Toolbar
     */
    public void setFragment(Fragment fragment,String title) {
        setFragment(fragment,title,null);

        if(!isCurrentFragmentChild) {// remove the last child from the stack
            childFragmentStack.remove(childFragmentStack.size() - 1);
            childTitleStack.remove(childTitleStack.size() - 1);
        }
        else for(int i = childFragmentStack.size()-1;i >= 0;i++) { // if a section is clicked when user is into a child remove all childs from stack
            childFragmentStack.remove(i);
            childTitleStack.remove(i);
        }

        // add to the childStack the Fragment and title
        childFragmentStack.add(fragment);
        childTitleStack.add(title);

        isCurrentFragmentChild = false;
    }

Perhaps the loop need to be:

 for(int i = childFragmentStack.size()-1;i >= 0;i--)
@neokree
Copy link
Owner

neokree commented Mar 5, 2015

I think that the real problem is another: in your changeFragment code, you have called setFragment and then setSection.
If you want to call a section programmatically use setSection.
If you want to call another fragment that is not a target of a section, call setFragment.
If you want to add a child Fragment in your section branch, call setFragmentChild.

Your code can be minimized to this:

public void selectSection(final Class<? extends BaseFragment> targetFragmentClazz) {
        setSection(getSection(targetFragmentClass));
    }

Don't complicate your life if you can

@vangliana
Copy link
Author

@neokree setSection actually never worked for me. Nothing happens if I call setSection.
I know I'm using it in my code but only to avoid any synchronisation issues because your documentation says to use it to change section.

My minimum target is API 18 and i'm using the latest appcompat-v7:21.0.+

@neokree
Copy link
Owner

neokree commented Mar 6, 2015

setSection calls the onClick method, so It is like if the user clicks on the section.

@egisxxx
Copy link

egisxxx commented Mar 7, 2015

@neokree my problem are same. I call setSection(getSectionByTitle("About")), but nothing happens. I investigated the code and finded, that fragment are replacing when occur public void onDrawerClosed(View view), But it will never be because i call setSection, not close drawer. I think, that it is bug.
After setSection calling, i open and close drawer, then fragment is changed

@vangliana
Copy link
Author

@neokree I think @egisxxx is right. I got to the same conclusion as to why setSection would not respond.

I also think that the way you clear the child section stack as mentioned in my first comment is problematic.

@neokree neokree added the bug label Mar 8, 2015
neokree pushed a commit that referenced this issue Mar 8, 2015
@neokree neokree added the testing label Mar 8, 2015
@neokree
Copy link
Owner

neokree commented Mar 8, 2015

I resolved the two problems with a bug fix.

@egisxxx
Copy link

egisxxx commented Mar 8, 2015

good news. But setSection will works only with fragments? what will happen, if will be used intent or custom target?

@ghost
Copy link

ghost commented Mar 23, 2015

@neokree I also have issue with setFragment method, it gives me java.lang.IndexOutOfBoundsException.

Here is my code on MainActivity :

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_create_post:
                    this.setFragment(new CreatePostFragment(),"Create Post");

                return true;
        }
        return super.onOptionsItemSelected(item);
 } 

@vangliana
Copy link
Author

@neokree I just tried release 1.3.3 and setSection still doesn't work. I can see the code was changed for this method, but can you please confirm whether it was fixed?

Also, I have some logic that sits in MaterialNavigationDrawer#onClick method that keeps track of which section has been selected and updates the toolbar and drawer header accordingly. Will onClick still be called when setting the section programatically? if not, where would be the best place to check which section has been selected?

Cheers

@vangliana
Copy link
Author

@neokree I figured where the problem is and raised this defect: #248

@neokree neokree closed this as completed Dec 22, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants