gpt4 book ai didi

android - 如何访问菜单项?

转载 作者:可可西里 更新时间:2023-11-01 19:10:07 26 4
gpt4 key购买 nike

所以我试图从 ActionBar 中获取我的菜单项并将它们设置为一些变量以供以后使用。下面是一些基本的测试代码,尝试在创建选项菜单期间设置变量。当它启动时崩溃并出现错误:

02-18 12:10:08.109: E/AndroidRuntime(30931): FATAL EXCEPTION: main
02-18 12:10:08.109: E/AndroidRuntime(30931): Process: com.example.slider2, PID: 30931
02-18 12:10:08.109: E/AndroidRuntime(30931): java.lang.IndexOutOfBoundsException: Invalid index 2131230720, size is 1
02-18 12:10:08.109: E/AndroidRuntime(30931): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
02-18 12:10:08.109: E/AndroidRuntime(30931): at java.util.ArrayList.get(ArrayList.java:308)
02-18 12:10:08.109: E/AndroidRuntime(30931): at com.android.internal.view.menu.MenuBuilder.getItem(MenuBuilder.java:656)
02-18 12:10:08.109: E/AndroidRuntime(30931): at com.example.slider2.MainActivity.onCreateOptionsMenu(MainActivity.java:22)
02-18 12:10:08.109: E/AndroidRuntime(30931): at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
02-18 12:10:08.109: E/AndroidRuntime(30931): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
02-18 12:10:08.109: E/AndroidRuntime(30931): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
02-18 12:10:08.109: E/AndroidRuntime(30931): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
02-18 12:10:08.109: E/AndroidRuntime(30931): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
02-18 12:10:08.109: E/AndroidRuntime(30931): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
02-18 12:10:08.109: E/AndroidRuntime(30931): at android.view.Choreographer.doFrame(Choreographer.java:543)
02-18 12:10:08.109: E/AndroidRuntime(30931): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
02-18 12:10:08.109: E/AndroidRuntime(30931): at android.os.Handler.handleCallback(Handler.java:733)
02-18 12:10:08.109: E/AndroidRuntime(30931): at android.os.Handler.dispatchMessage(Handler.java:95)
02-18 12:10:08.109: E/AndroidRuntime(30931): at android.os.Looper.loop(Looper.java:136)
02-18 12:10:08.109: E/AndroidRuntime(30931): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-18 12:10:08.109: E/AndroidRuntime(30931): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 12:10:08.109: E/AndroidRuntime(30931): at java.lang.reflect.Method.invoke(Method.java:515)
02-18 12:10:08.109: E/AndroidRuntime(30931): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-18 12:10:08.109: E/AndroidRuntime(30931): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-18 12:10:08.109: E/AndroidRuntime(30931): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
02-18 12:10:08.109: E/AndroidRuntime(30931): at dalvik.system.NativeStart.main(Native Method)

MainActivity.java

public class MainActivity extends Activity {

private MenuItem mRefresh;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
mRefresh = menu.getItem(R.id.refresh);
return super.onCreateOptionsMenu(menu);
}
}

ma​​in_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/refresh"
android:icon="@drawable/ic_action_refresh"
android:showAsAction="ifRoom"
android:title="@string/refresh"/>
</menu>

关于我在这里做错了什么有什么想法吗?我需要能够操纵菜单项,而不仅仅是在它们被点击时。

最佳答案

MenuItem.getItem(index)采用菜单项的索引而不是菜单项的 id,因此使用 MenuItem.findItem 将菜单项 id 作为:

 mRefresh = menu.findItem(R.id.refresh); //item id
OR
mRefresh = menu.getItem(0); //item index

关于android - 如何访问菜单项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21864502/

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