4

I want to change my menu dynamically, so I tried this but my app crash :

To get my menu in all my activity :

private Menu menu;

Then in onCreateOptionsMenu before return statement:

this.menu = menu;

And then in a function :

MenuItem parent = menu.findItem(R.id.parent_menu);
parent.setTitle("test");

Crashing... I founded the same answer I did : Android - How to dynamically change menu item text outside of onOptionsItemsSelected or onCreateOptionsMenu but for me, not working.

Thanks in advance

PS : If MenuItem parent and setTitle are in commentary, it's works fine.


So the problem is onCreateOptionsMenu is called only when I want to view the menu for the first time... How can I call it before ?

04-20 17:00:19.170: D/dalvikvm(26887): GC_EXTERNAL_ALLOC freed 45K, 50% free 2729K/5379K, external 0K/0K, paused 27ms
04-20 17:00:19.230: D/dalvikvm(26887): GC_EXTERNAL_ALLOC freed 11K, 49% free 2747K/5379K, external 565K/576K, paused 33ms
04-20 17:00:19.300: D/dalvikvm(26887): GC_EXTERNAL_ALLOC freed 62K, 49% free 2787K/5379K, external 824K/1091K, paused 22ms
04-20 17:00:19.825: D/dalvikvm(26887): GC_CONCURRENT freed 314K, 50% free 2859K/5639K, external 1521K/1630K, paused 2ms+3ms
04-20 17:00:19.845: I/webclipboard(26887): clipservice: android.sec.clipboard.ClipboardExManager@40514930
04-20 17:00:20.145: D/WML_SISO(26887): InitPasteboardJni
04-20 17:00:20.185: D/dalvikvm(26887): GC_CONCURRENT freed 323K, 48% free 3062K/5831K, external 1591K/1630K, paused 3ms+2ms
04-20 17:00:20.210: D/CLIPBOARD(26887): Hide Clipboard dialog at Starting input: finished by someone else... !
04-20 17:00:20.340: D/dalvikvm(26887): GC_CONCURRENT freed 179K, 44% free 3423K/6087K, external 1425K/1630K, paused 1ms+2ms
04-20 17:00:21.935: D/AndroidRuntime(26887): Shutting down VM
04-20 17:00:21.935: W/dalvikvm(26887): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
04-20 17:00:21.940: E/AndroidRuntime(26887): FATAL EXCEPTION: main
04-20 17:00:21.940: E/AndroidRuntime(26887): java.lang.NullPointerException
04-20 17:00:21.940: E/AndroidRuntime(26887):    at p.NewsRecovery.AddWebsiteActivity$1.onClick(AddWebsiteActivity.java:133)
04-20 17:00:21.940: E/AndroidRuntime(26887):    at android.view.View.performClick(View.java:2538)
04-20 17:00:21.940: E/AndroidRuntime(26887):    at android.view.View$PerformClick.run(View.java:9152)
04-20 17:00:21.940: E/AndroidRuntime(26887):    at android.os.Handler.handleCallback(Handler.java:587)
04-20 17:00:21.940: E/AndroidRuntime(26887):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 17:00:21.940: E/AndroidRuntime(26887):    at android.os.Looper.loop(Looper.java:130)
04-20 17:00:21.940: E/AndroidRuntime(26887):    at android.app.ActivityThread.main(ActivityThread.java:3691)
04-20 17:00:21.940: E/AndroidRuntime(26887):    at java.lang.reflect.Method.invokeNative(Native Method)
04-20 17:00:21.940: E/AndroidRuntime(26887):    at java.lang.reflect.Method.invoke(Method.java:507)
04-20 17:00:21.940: E/AndroidRuntime(26887):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
04-20 17:00:21.940: E/AndroidRuntime(26887):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
04-20 17:00:21.940: E/AndroidRuntime(26887):    at dalvik.system.NativeStart.main(Native Method)
04-20 17:00:22.015: D/dalvikvm(26887): GC_EXTERNAL_ALLOC freed 586K, 50% free 3142K/6215K, external 1511K/1630K, paused 38ms
04-20 17:00:22.065: D/szipinf(26887): Initializing inflate state
04-20 17:00:22.065: D/szipinf(26887): Initializing zlib to inflate
04-20 17:00:22.170: D/dalvikvm(26887): GC_FOR_MALLOC freed 222K, 50% free 3282K/6535K, external 1933K/2370K, paused 17ms
04-20 17:00:22.170: I/dalvikvm-heap(26887): Grow heap (frag case) to 7.591MB for 129176-byte allocation
04-20 17:00:22.190: D/dalvikvm(26887): GC_FOR_MALLOC freed 18K, 50% free 3390K/6663K, external 1586K/2098K, paused 12ms
04-20 17:00:22.210: D/dalvikvm(26887): GC_FOR_MALLOC freed 134K, 51% free 3319K/6663K, external 1586K/2098K, paused 11ms
04-20 17:00:22.210: I/dalvikvm-heap(26887): Grow heap (frag case) to 7.412MB for 258336-byte allocation
04-20 17:00:22.230: D/dalvikvm(26887): GC_FOR_MALLOC freed 8K, 49% free 3563K/6919K, external 1586K/2098K, paused 11ms
04-20 17:00:22.255: D/dalvikvm(26887): GC_FOR_MALLOC freed 0K, 49% free 3563K/6919K, external 1586K/2098K, paused 12ms
04-20 17:00:22.255: I/dalvikvm-heap(26887): Grow heap (frag case) to 7.527MB for 129176-byte allocation
04-20 17:00:22.270: D/dalvikvm(26887): GC_FOR_MALLOC freed 253K, 52% free 3435K/7047K, external 1556K/2068K, paused 14ms
Community
  • 1
  • 1
Shinao
  • 137
  • 4
  • 13
  • Oh sorry, I edited my post, and if you find the error, can you tell me how did you do to "decrypt" the logcat ? Thanks – Shinao Apr 20 '12 at 15:04
  • So the problem is onCreateOptionsMenu is called only when I want to view the menu for the first time... How can I call it before ? – Shinao Apr 20 '12 at 16:20
  • check line no. 133 of AddWebsiteActivity.java ,,,tell me... – Samir Mangroliya Apr 20 '12 at 16:54
  • Line 133 : MenuItem parent = menu.findItem(R.id.parent_menu); ... But I know what the problem is : I can't do this.menu = menu; in onCreateOptionsMenu() because this function is called the first time I click on the touch menu, so if I don't at the beginning, my app crash... How I can call it programmaly ? – Shinao Apr 20 '12 at 17:55
  • something is null ....i mean menu is null try to youractivity.this.name.menu and get value it check `if(menu!=null) and also check before create menu you try use it!!!` – Samir Mangroliya Apr 20 '12 at 18:02
  • I know, menu is null because menu is initialized the first time I open the menu option, but I don't do it... (Read my commentary). My solution is to do that : openOptionsMenu(); closeOptionsMenu(); And then menu is not null, but this is ugly and I will love an other way. – Shinao Apr 20 '12 at 22:32

1 Answers1

0

You must inflate the menu before setting it to your variable;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_visit_profile, menu);
    mOptionsMenu = menu;
    return super.onCreateOptionsMenu(menu);
}

And then you can use it.

MenuItem item = mOptionsMenu.findItem(R.id.visit_profile_hi);
item.setTitle("Remover hi");
Danilo Lemes
  • 2,342
  • 1
  • 14
  • 16