menuitem.getmenuinfo is null in onContextItemSelected

1.1k views Asked by At

I have a ScrollView with an embedded LinearLayout, as suggested in the ScrollView documentation. The following is a reduction of the layout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab2_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>

</ScrollView>

I use the following pattern in many Activities that use a ListView instead of a ScrollView, with no issue. So, the issue seems unique to ScrollView.

final ScrollView scrollView = (ScrollView) frame.findViewById(R.id.tab2_view); 
final LinearLayout layout = (LinearLayout) frame.findViewById(R.id.top); 

//load and sort backing ArrayList

//neither of these calls resolve the issue
registerForContextMenu(scrollView);
//registerForContextMenu(layout);

//load layout with views

Here's where the issue occurs. The value returned from item.getMenuInfo() is always null.

@Override
    public boolean onContextItemSelected(MenuItem item){
        boolean result = false;
        try 
        {
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); //why is this null???
            switch (item.getItemId()) {
                //do stuff
            }
        }
        catch (Exception e) { ExceptionHandler.handleException(LOG_TAG, e); }
        return result;
    }

Does anyone know how to resolve the issue?

I did find a work around in maintaining state variables, and checking against the menuItem's title instead of the null AdapterContextMenuInfo. But, I would prefer to understand how to get the AdapterContextMenuInfo from the selected MenuItem.

I've been through the SO posts. They all seem to be rooted in copy/paste errors, or not calling registerForContextMenu on the correct object. This is the only article that suggests there's an issue aside from this.

0

There are 0 answers