我想动态更改我的菜单,所以我尝试了这个,但我的应用程序崩溃了:
要在我的所有活动中获取我的菜单:
private Menu menu;
然后在返回语句之前的onCreateOptionsMenu中:
this.menu = menu;
然后在一个函数中:
MenuItem parent = menu.findItem(R.id.parent_menu);
parent.setTitle("test");
崩溃......我找到了同样的答案:Android - How to dynamically change menu item text outside of onOptionsItemsSelected or onCreateOptionsMenu但是对我来说,没有工作。
提前致谢
PS:如果MenuItem parent和setTitle在评论中,它可以正常工作。
所以问题是onCreateOptionsMenu仅在我想第一次查看菜单时被调用...我怎么能在之前调用它?
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
答案 0 :(得分:0)
在将菜单设置为变量之前,您必须对菜单进行充气;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_visit_profile, menu);
mOptionsMenu = menu;
return super.onCreateOptionsMenu(menu);
}
然后你可以使用它。
MenuItem item = mOptionsMenu.findItem(R.id.visit_profile_hi);
item.setTitle("Remover hi");