0

i want to change some titles out of my Activites programmatically. I have some items like

"Show/Hide something1" "Show/Hide something2"

And now i want to change the text to:

"Show something1" and do some action, and change the text of this Menu item to:

"Hide something1" .....

Set title of an Android Item

i test this Solution, and get a null Pointer

My Layout:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:universal="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/menu_1"
        universal:showAsAction="never"
        android:title="Show/Hide Something1" />

    <item
        android:id="@+id/menu_2"
        universal:showAsAction="never"
        android:title="Show/Hide Something2" />

</menu>

And my Code:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.rallye_menu, menu);
        MenuItem item=menu.getItem(R.id.menu_1); // here itemIndex is int
        item.setTitle("YourTitle");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_1:
                // do some action
                return true;
            case R.id.menu_2:
                // doe other action
                return true;
                default:
                return super.onOptionsItemSelected(item);
        }
}

Got this Error:

Process: com.packagename.foo, PID: 12441 java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.MenuItem.setTitle(java.lang.CharSequence)' on a null object reference at com.sherdle.universal.rallye.MainActivity.onCreateOptionsMenu(MainActivity.java:646)

in this line: MenuItem item=menu.getItem(R.id.menu_1)

Need help :)

EDIT: found another Solution: Android - How to dynamically change menu item text outside of onOptionsItemsSelected or onCreateOptionsMenu but does not work

thefab
  • 59
  • 1
  • 11

1 Answers1

1

Because Menu's getItem() method returns a menu item at a given index as opposed to passing in a resource ID which can potentially throw an IndexOutOfBoundsException as mentioned here in the docs. Instead, you should be using index values so it'd be 0 for the first menu item, menu_1, and 1 for the other menu item, menu_2.

DaveNOTDavid
  • 1,753
  • 5
  • 19
  • 37
  • Oh, damn MenuItem item=menu.getItem(R.id.menu_1); // here itemIndex is int i should read the annotations carefully :-O thanks! now it works with in index Is this "faster": menu.findItem(R.id.menu_2); menu1.setTitle("Text"); which method is better? via findItem or getItem? – thefab Jun 26 '17 at 20:58
  • @georf Not really sure which is "faster", but I'd recommend findItem() since it's good practice to reference a View by its resource ID. – DaveNOTDavid Jun 26 '17 at 21:24
  • okay thanks! Will use findByItem it seems the best version thanks for help =) – thefab Jun 27 '17 at 11:11