1

I click an Icon on the action bar to display a popup menu, then I click a popup menu item and a toast message in my Java code shows that it changes state from false to true. The problem is that when the popup menu is opened again no items are clicked. And clicking a popup menu item always shows that it changes state from false to true.

Does anyone have a code solution for this problem?

Thank you for your help.

Java code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.

    getMenuInflater().inflate(R.menu.menu_action, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    int id = item.getItemId();

    if (id == R.id.action_popup) {

        View menuItemView = findViewById(R.id.action_popup);
        PopupMenu popupMenu = new PopupMenu(this, menuItemView);
        MenuInflater inflater = popupMenu.getMenuInflater();
        inflater.inflate(R.menu.popup_menu, popupMenu.getMenu());

        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

            public boolean onMenuItemClick(MenuItem item) {

                int id = item.getItemId();

                if (id == R.id.opt1_name) {

                    if (item.isChecked()){
                        item.setChecked(false);
                        Toast.makeText(getApplicationContext(), "Logic Set False= " + item, Toast.LENGTH_SHORT).show();
                    }
                    else{
                        item.setChecked(true);
                        Toast.makeText(getApplicationContext(), "Logic Set True= " + item, Toast.LENGTH_SHORT).show();
                    }


                }

                    else if (id == R.id.opt2_date) {

                    if (item.isChecked()){item.setChecked(false);
                        Toast.makeText(getApplicationContext(), "Logic Set False= " + item, Toast.LENGTH_SHORT).show();
                    }
                    else{item.setChecked(true);
                        Toast.makeText(getApplicationContext(), "Logic Set True= " + item, Toast.LENGTH_SHORT).show();
                    }

                }

                return true;
            }
        });

        popupMenu.show();
    }

return super.onOptionsItemSelected(item);
}

menu_action.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".PlayerActivity">

        <item android:id="@+id/action_popup" android:title="Sort Popup" android:icon="@drawable/ic_sort"
            android:showAsAction="always" />

        <item android:id="@+id/action_settings" android:title="Settings"
            app:showAsAction="never" />

        <item android:id="@+id/action_help" android:title="Help"
            app:showAsAction="never" />

</menu>

popup_menu.xml

<?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">

        <group android:checkableBehavior="single">
            <item
                android:id="@+id/opt1_name"
                android:title="Name" />

            <item
                android:id="@+id/opt2_date"
                android:title="Date" />
        </group>

    </menu>
user2308699
  • 239
  • 1
  • 5
  • 14
  • see this: http://stackoverflow.com/questions/3040374/runtimeexception-your-content-must-have-a-listview-whose-id-attribute-is-andro – rafsanahmad007 Mar 10 '17 at 17:54
  • Please ignore the Logcat output, it was posted by mistake and is not related to this post. The app is not showing any output in Logcat. – user2308699 Mar 10 '17 at 18:20

1 Answers1

0

Check the error if you examine then you must have one listview in your mainlist.xml file with id as @android:id/list

<ListView
    android:id="@android:id/list"
    android:layout_height="wrap_content"
    android:layout_height="fill_parent"/>
josedlujan
  • 5,357
  • 2
  • 27
  • 49
  • Please ignore the Logcat output, it was posted by mistake and is not related to this post. The app is not showing any output in Logcat. – user2308699 Mar 10 '17 at 18:18