Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access menu items?

Tags:

android

So I'm trying to grab my menu items off the ActionBar and set them into some variables to use later. Below is some basic test code that tries to set the variable during the creation of the options menu. When the it launches it crashes with the error:

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);
    }
}

main_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>

Any ideas on what I'm doing wrong here? I need to be able to manipulate the menu items beyond just when they are being tapped.

like image 808
Devin Rodriguez Avatar asked Feb 18 '14 20:02

Devin Rodriguez


People also ask

How do I get menu item view?

If you have access to and use AppCompat's Toolbar there is a way. It's not the most efficient way, but it's the easiest way I've found to access the menu item's view. public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { Toolbar toolbar = (Toolbar) view. findViewById(R.

How do I access menu items on Android?

Here's a quick example of how to access an Android MenuItem in a Java Activity or Fragment class (i.e., in your Java code). you can access the MenuItem with the id menuItemPinQuote like this in your Android/Java code: public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) { menuInflater. inflate(R.

How do you respond to a menu item?

Responding to Menu Item Clicks When a menu item is clicked, Android calls the onOptionsItemSelected callback method of the Activity class by passing a reference to the clicked menu item. You then use the getItemId() method on the MenuItem to see which item it is.

What do you mean by menu in Android?

In android, Menu is an important part of the UI component which is used to provide some common functionality around the application. With the help of menu, users can experience a smooth and consistent experience throughout the application.


1 Answers

MenuItem.getItem(index) take index of the menu item instead of id of menu item so use MenuItem.findItem which take menu item id as:

 mRefresh = menu.findItem(R.id.refresh); //item id
  OR
 mRefresh = menu.getItem(0); //item index
like image 185
ρяσѕρєя K Avatar answered Sep 21 '22 09:09

ρяσѕρєя K