Search code examples
javaandroid-studiochromecast

Cannot cast 'ActionProvider' to 'MediaRouteActionProvider'


I'm creating my first Chromecast app and following this tutorial I'm having trouble in my onCreateOptionsMenu method:

 public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.menu_main, menu);
    MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
    MediaRouteActionProvider mediaRouteActionProvider =
            (MediaRouteActionProvider)MenuItemCompat.getActionProvider(mediaRouteMenuItem);

    mediaRouteActionProvider.setRouteSelector(mMediaRouteSelector);
    return true;
}

It's throwing an error:

Incontrovertible types cannot cast 'android.support.v4.view.ActionProvider' to 'android.app.MediaRouteActionProvider'

I've extended AppCompatActivity rather than ActionBarActivity, I can't find a solution to this.


Solution

  • Make sure you are importing MediaRouter from the MediaRouter support library and not the one from the android framework itself.