gpt4 book ai didi

android - 在他的电话之外获取菜单

转载 作者:太空狗 更新时间:2023-10-29 15:18:31 26 4
gpt4 key购买 nike

我想动态更改我的菜单,所以我尝试了这个但是我的应用程序崩溃了:

在我的所有 Activity 中获取我的菜单:

private Menu menu;

然后在 onCreateOptionsMenu 之前的 return 语句:

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

最佳答案

您必须在将菜单设置为您的变量之前对其进行扩充;

@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");

关于android - 在他的电话之外获取菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10248448/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com