Khi tôi cố gắng cập nhật MenuItem trong menu, tôi nhận được IndexOutOfBoundsException.Trình đơn Android, ném IndexOutOfBoundsException trong khi chạy trênPrepareOptionsMenu
Tôi đã thêm menu_item vào xml và tôi có thể thấy nó khi bật() == false.
Mã của tôi:
public boolean onPrepareOptionsMenu(Menu menu) {
if (enable()) {
MenuItem menuItem= menu.getItem(R.id.menu_item);
menuItem.setEnabled(true);
}
return super.onPrepareOptionsMenu(menu);
}
Làm thế nào để đối phó với vấn đề này?
Chúc mừng.
Bạn có thể muốn gọi 'super.onPrepareOptionsMenu' trước khi truy cập nội dung của nó. Thay vào đó, hãy sử dụng 'Menu.findItem'. – harism
Ok, sau khi thay đổi getItem thành findItem, ứng dụng không có ngoại lệ. Làm việc như một say mê. – neciu