Angelo
Angelo

Reputation: 13

android - my SwitchCompat cannot be cast to SwitchCompat

I'm quite new to android and AndroidStudio, and I'm having a problem with a SwitchCompat. I need it to be on my NavigationDrawer menu, and I just want to check its isChecked value. After some tutorials I managed to put this code together: this is my SwitchCompat XML, drawer_switch.xml

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.SwitchCompat 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_switch"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    app:showText="false" />

Then I have the NavigationView in my DrawerLayout activity_main.xml

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"

        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_menu" />

and my drawer_menu.xml with its SwitchCompat

<?xml version="1.0" encoding="utf-8"?>
<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:showIn="navigation_view">
    <item android:title="Sondaggi"
        android:id="@+id/menuSondaggi">
        <menu>
            <item
                android:id="@+id/set_votato"
                android:icon="@drawable/ic_set_votato"
                android:title="Mostra già votati"
                app:actionLayout="@layout/drawer_switch" />
                [...]
         </menu>
    </item>
</menu>

Now, in my MainActivity, at the OnCreate, i'm trying to get the SwitchCompat in order to check its isChecked:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        try {
            MenuItem item = (MenuItem) navigationView.getMenu().findItem(R.id.set_votato);
            SwitchCompat votatoSwitch = (SwitchCompat) item;
            votatoSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    votato=isChecked;
                }
            });
        }catch (Exception e){
            e.getCause();
        }
        [...]
    }

But it gets stuck when it tries to cast item to a SwitchCompat, saying java.lang.ClassCastException: android.support.v7.view.menu.MenuItemImpl cannot be cast to android.support.v7.widget.SwitchCompat. The fact is that if I debug, I can see that the item knows to be a SwitchCompat, as you can see from the image: Debug screenshot

Where's my mistake? Than you in advance.

Upvotes: 1

Views: 1997

Answers (2)

1punct168
1punct168

Reputation: 11

The set_votato is a MenuItem you should try item.getActionView() and cast that to a switchCompat

Upvotes: 1

TheWanderer
TheWanderer

Reputation: 17824

item is a MenuItem. The View class doesn't extend the MenuItem class, so you can't cast it to a View.

I think you want the MenuItem#getActionView() method. This returns the inflated layout you set in android:actionLayout.

SwitchCompat votatoSwitch = (SwitchCompat) item.getActionView();

Upvotes: 1

Related Questions