Khrystyna Pochynok
Khrystyna Pochynok

Reputation: 248

ConcurrentModificationException - Unable to start activity

Have crash in Crashlytics which I cannot reproduce and don't know where it occurs. Posting all received logs below.

Fatal Exception: java.lang.RuntimeException 
Unable to start activity ComponentInfo{.main.MainActivity}: java.util.ConcurrentModificationException 
Caused by java.util.ConcurrentModificationException
java.util.ArrayList$ArrayListIterator.next (ArrayList.java:573)
android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.restoreInstanceState (NavigationMenuPresenter.java:587)
android.support.design.internal.NavigationMenuPresenter.onRestoreInstanceState (NavigationMenuPresenter.java:191)

As you can see from logs NavigationMenuPresenter and NavigationMenuAdapter are classes in android.support.design.internal. From the names of it looks like it has something to do with Navigation Drawer menu and some ArrayList (correct me if I'm wrong). Does some error occur when trying to restore state of menu?

Here is code for navigation menu creation

private void setupDrawer(Toolbar toolbar) {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        initAvailabilityChange(navigationView);
        initMenuHeaderViews(navigationView);
    }

    private void initAvailabilityChange(NavigationView navigationView) {
        switchItem = navigationView.getMenu().findItem(R.id.availability);
        CompoundButton switchView = (CompoundButton) MenuItemCompat.getActionView(switchItem);
        switchNew = (Switch) switchView.findViewById(R.id.switchLocation);
        switchView.setOnCheckedChangeListener((buttonView, isChecked) -> {
            isOnShift = isChecked;
            presenter.onAvailabilityChange(isOnShift, this);
            if (isChecked) {
                switchItem.setTitle(R.string.availability_on_shift);
            } else {
                switchItem.setTitle(R.string.availability_off_shift);
            }
        });
    }

    private void initMenuHeaderViews(NavigationView navigationView) {
        View header = navigationView.getHeaderView(0);
        tvName = (TextView) header.findViewById(R.id.tvName);
        tvRank = (TextView) header.findViewById(R.id.tvRank);
    }

Would be really grateful for any help.

Upvotes: 1

Views: 2281

Answers (1)

Bogdan Ustyak
Bogdan Ustyak

Reputation: 5837

It might be an issue with ViewGroup implementation. Try to post a new Runnable.

switchView.post(new Runnable() {
  public void run() {
    if (isChecked) {
                switchItem.setTitle(R.string.availability_on_shift);
            } else {
                switchItem.setTitle(R.string.availability_off_shift);
            }
  }
});

Upvotes: 2

Related Questions