IndexOutOfBoundsException:获取菜单项(溢出图标)项的标题

时间:2016-11-02 06:04:39

标签: android menuitem indexoutofboundsexception android-menu

我正在尝试为特定菜单项设置特定用户名,然后使用

获取
  LoggedUser = (String) menu.getItem(R.id.User).getTitle().toString();

喜欢:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    menu.findItem(R.id.User).setTitle("username");
    LoggedUse0r=(String) menu.getItem(R.id.User).getTitle().toString();
    invalidateOptionsMenu();
    return super.onPrepareOptionsMenu(menu);
}

我宣布了

invalidateOptionsMenu();

用不同的方法,如

onCreate(), onStart(), onResume()

然而,我的应用程序崩溃并给了我这个例外:

  

11-02 07:44:52.112 8714-8714 / com.example.prof_mohamedatef.listview   E / AndroidRuntime:致命异常:主要                                                                                        java.lang.IndexOutOfBoundsException:索引2131492986无效,大小为   4                                                                                            在   java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)                                                                                            at java.util.ArrayList.get(ArrayList.java:304)                                                                                            在   android.support.v7.view.menu.MenuBuilder.getItem(MenuBuilder.java:729)                                                                                            在   com.example.prof_mohamedatef.listview.Booking_hotels.onPrepareOptionsMenu(Booking_hotels.java:194)                                                                                            在android.app.Activity.onPreparePanel(Activity.java:2570)                                                                                            在   android.support.v4.app.FragmentActivity.onPrepareOptionsPanel(FragmentActivity.java:532)                                                                                            在   android.support.v4.app.FragmentActivity.onPreparePanel(FragmentActivity.java:521)                                                                                            在   android.support.v7.view.WindowCallbackWrapper.onPreparePanel(WindowCallbackWrapper.java:93)                                                                                            在   android.support.v7.app.AppCompatDelegateImplBase $ AppCompatWindowCallbackBase.onPreparePanel(AppCompatDelegateImplBase.java:355)                                                                                            在   android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1380)                                                                                            在   android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1633)                                                                                            在   android.support.v7.app.AppCompatDelegateImplV9.access $ 100(AppCompatDelegateImplV9.java:93)                                                                                            在   android.support.v7.app.AppCompatDelegateImplV9 $ 1.run(AppCompatDelegateImplV9.java:129)                                                                                            在android.os.Handler.handleCallback(Handler.java:725)                                                                                            在android.os.Handler.dispatchMessage(Handler.java:92)                                                                                            在android.os.Looper.loop(Looper.java:137)                                                                                            在android.app.ActivityThread.main(ActivityThread.java:5283)                                                                                            at java.lang.reflect.Method.invokeNative(Native Method)                                                                                            在java.lang.reflect.Method.invoke(Method.java:511)                                                                                            在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)                                                                                            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)                                                                                            在dalvik.system.NativeStart.main(本地方法)

任何机构都可以告诉我获取项目标题的字符串之间的关系 和这种类型的例外:

IndexOutOfBoundsException:

您的回复将不胜感激,提前致谢

2 个答案:

答案 0 :(得分:5)

试试这个。

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.your_menu, menu);

    MenuItem item = menu.findItem(R.id.your_menu_item);
    String loggedInUser = item.getTitle().toString();
    return true;
}

答案 1 :(得分:3)

您正在将View ID传递给menu.getItem(R.id.User)。根据文件

  

MenuItem getItem(int index)

     

获取给定索引处的菜单项。

所以改为使用它:

menu.getItem(0...3);    // since the size of menu items is 4.