Edit : Solved, I detailed the solution in an answer.
I might be missing something easy and I think the solution will be quite simple but I can't find it.
My problem is that I want to change the text of my menu items (displayed in a navigation drawer) dynamically, but when i try to use findItem(R.id.item1)
I get a null value.
Here is my code where I try to change the text :
// call invalidateOptionsMenu(); to refresh
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.item1).setTitle("Some random text");
return super.onPrepareOptionsMenu(menu);
}
And my menu :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="@+id/group_users"
android:checkableBehavior="single">
<item
android:id="@+id/item1"
android:icon="@drawable/util_bleu_24dp"
android:title="@string/item1"/>
<item
android:id="@+id/item2"
android:icon="@drawable/util_rose_24dp"
android:title="@string/item2"/>
</group>
When I try to execute it I get the error :
java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.MenuItem.setTitle(java.lang.CharSequence)' on a null object reference
Can someone help me find what's wrong with this please ?