Ive run through all the steps in the basic set up of the chromecast sender app at https://developers.google.com/cast/docs/android_sender?hl=ko
I keep getting a nullpointer in my onCreateOptions menu here:
MediaRouteActionProvider mediaRouteActionProvider =
(MediaRouteActionProvider) MenuItemCompat.getActionProvider(mediaRouteMenuItem);
mediaRouteActionProvider.setRouteSelector(this.mediaRouteSelector);
When I debug and step through i can see that the mActionProvider in the menu item is null.
However I set up the menu exactly like it is in the sample using
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
>
<item
android:id="@+id/media_route_menu_item"
android:title="@string/media_route_menu_title"
android:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
android:showAsAction="always"/>
<item
heres my instantiating of the mediarouter in my oncreate
mMediaRouter = MediaRouter.getInstance(getApplicationContext());
mediaRouteSelector = new MediaRouteSelector.Builder()
.addControlCategory(CastMediaControlIntent.categoryForCast(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID))
.build();
Im also using an activity that extends from ActionBarActivity and Ive import all the libraries needed. Ive seen this is a common problem but I havent seen a common fix yet. Is there some new flag or something Im missing to get this working?
To any one looking up a similar npe as the one I was getting here is the solution that fixed my problem. I was running into a null pointer here:
MediaRouteActionProvider mediaRouteActionProvider =
(MediaRouteActionProvider) MenuItemCompat.getActionProvider(mediaRouteMenuItem);
When I did some debugging I could see that even though all my set up was correct that for some reason in my project something was causing the action provider of the menuitem to always be null. My solution which works for me was to just set one on the fly and then I was able to finally get the device popup to show with the following code.
MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
MediaRouteActionProvider mediaRouteActionProvider =
(MediaRouteActionProvider) MenuItemCompat.getActionProvider(mediaRouteMenuItem);
if(mediaRouteActionProvider==null){
mediaRouteActionProvider = new MediaRouteActionProvider(this);
MenuItemCompat.setActionProvider(mediaRouteMenuItem, mediaRouteActionProvider);
}
mediaRouteActionProvider.setRouteSelector(this.mediaRouteSelector);
mediaRouteActionProvider.setDialogFactory(new MediaRouteDialogFactory() {
@Override
public MediaRouteControllerDialogFragment onCreateControllerDialogFragment() {
return new MediaRouteControllerDialogFragment() {
@Override
public MediaRouteControllerDialog onCreateControllerDialog(
Context context, Bundle savedInstanceState) {
MediaRouteControllerDialog mControllerDialog =
new MediaRouteControllerDialog(TrailerPlayer.this);
return mControllerDialog;
}
};
}
});