0

Keep getting error with keyword logout in switch statement as as cannot resolve symbol logout. want to put icon on action bar but don't know how to setup action bar. help me setup action bar resolve this issue as well please. thanks

@Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        //handle presses on the action bar items
        switch (item.getItemId())
        {
            case R.id.logout:
            {
                AlertDialog.Builder builder=new AlertDialog.Builder(this);
                builder.setMessage("Do you want to logout?").setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int id) {
                                startActivity(new Intent(Attendence.this,ChoosePanel.class));
                            } //public void onClick(DialogInterface dialog, int id)
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                //action for 'NO' button
                                dialog.cancel();
                            } //public void onClick(DialogInterface dialog, int id)
                        });
                //creatin dialog box
                AlertDialog alert=builder.create();
                //setting the title manually
                alert.setTitle("LOGOUT");
                alert.show();
                return true;
            }       
  • Please Include more code; what is R? Is it a class? Please show the code for it. – Aly Mobarak Apr 28 '22 at 22:24
  • Does this answer your question? [What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-or-cannot-resolve-symbol-error-mean) – Turing85 Apr 28 '22 at 22:26

1 Answers1

0

To create an action bar you have to create a resource file of resource type "Menu".

In that file you can have as much options as you want and can define them to be shown on the bar or ou the "three got" menu. Here is an example:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/logout"
    android:icon="@drawable/ic_logout"
    android:title="@string/sign_out"
    app:showAsAction="ifRoom"/>
<item
    android:id="@+id/settings"
    android:icon="@drawable/abc_vector_test"
    android:title="@string/settings" />
</menu>

Note the @+id in the android:id definition. Note as well that if you add app:showAsAction="ifRoom" the option will be shown on the bar instead of the dropdown menu.

Then, in your activity, add the onCreateOptionsMenu(Menu menu) method. Something like:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);

    return super.onCreateOptionsMenu(menu);
}

Then the function that you showed in your question will catch the menu click events.

Manuel Tomás
  • 540
  • 3
  • 11