ConcurrentModificationException - 无法启动 activity
ConcurrentModificationException - Unable to start activity
在 Crashlytics 中发生崩溃,我无法重现并且不知道发生在何处。在下面发布所有收到的日志。
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)
从日志中可以看出,NavigationMenuPresenter 和 NavigationMenuAdapter 在 android.support.design.internal 中 类。从它的名字看起来它与 Navigation Drawer 菜单和一些 ArrayList 有关(如果我错了请纠正我)。尝试恢复菜单状态时是否发生错误?
这是创建导航菜单的代码
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);
}
非常感谢任何帮助。
这可能是 ViewGroup 实现的问题。尝试 post 一个新的 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);
}
}
});
在 Crashlytics 中发生崩溃,我无法重现并且不知道发生在何处。在下面发布所有收到的日志。
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)
从日志中可以看出,NavigationMenuPresenter 和 NavigationMenuAdapter 在 android.support.design.internal 中 类。从它的名字看起来它与 Navigation Drawer 菜单和一些 ArrayList 有关(如果我错了请纠正我)。尝试恢复菜单状态时是否发生错误?
这是创建导航菜单的代码
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);
}
非常感谢任何帮助。
这可能是 ViewGroup 实现的问题。尝试 post 一个新的 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);
}
}
});