Search code examples
javaandroidgoogle-cast

Google Cast Remote Display Crash (Selector must not be null)


I am making a remote display for a gallery app and I am getting a very strange error that seems unrelated to the cast at all. At first, it crashed because of an invalid App ID, but after registering it, and getting an App ID, it crashes and says that the selector must not be null, but the Cast part is not in the same code that calls Null. Any help?

//Inside of OnCreate
MediaRouter = MediaRouter.getInstance(getApplicationContext());
    MediaRouteSelector mMediaRouteSelector = new MediaRouteSelector.Builder()
            .addControlCategory( CastMediaControlIntent.categoryForCast(getString(R.string.cast_sdk_id)))
            .build();



//Inside of OnCreateOptionsMenu
 public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_viewer, menu);
    if (mAdapter.getEntries().size() > 0) {
        MediaEntry currentEntry = mAdapter.getEntries().get(mCurrentPosition);
        if (currentEntry == null || currentEntry.isVideo()) {
            menu.findItem(R.id.print).setVisible(false);
            menu.findItem(R.id.edit).setVisible(false);
            menu.findItem(R.id.set_as).setVisible(false);
        } else {
            menu.findItem(R.id.print).setVisible(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT);
            menu.findItem(R.id.edit).setVisible(true);
            menu.findItem(R.id.set_as).setVisible(true);
        }
    }
    menu.findItem(R.id.slideshow).setVisible(!mAllVideos && mSlideshowTimer == null);
    MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
    MediaRouteActionProvider mediaRouteActionProvider =
            (MediaRouteActionProvider) MenuItemCompat.getActionProvider(mediaRouteMenuItem);
    mediaRouteActionProvider.setRouteSelector(mMediaRouteSelector);

    return super.onCreateOptionsMenu(menu);;



java.lang.IllegalArgumentException: selector must not be null
                                                                                 at android.support.v7.app.MediaRouteActionProvider.setRouteSelector(MediaRouteActionProvider.java:169)
                                                                                 at com.afollestad.impression.viewer.ViewerActivity.onCreateOptionsMenu(ViewerActivity.java:804)

Solution

  • When you state that a certain line of code is throwing an exception, it is useful to indicate which line of the code it is since we don't have your full source. Regarding your issue, in your onCreate), as it is stated in your post, you have MediaRouteSelector mMediaRouteSelector = .... Since we don't have your source code, I am going to assume that it is really as you have copied here. Since you also use mMediaRouteSelector in onCreateOptionsMenu(), it sounds like you have an instance variable mMediaRouteSelector which is shadowed in your onCreate(); in other words, the assignment made in onCreate() is local to that method and not global, hence it is practically null in onCreateOptionsMenu(). Change the assignment in onCreate() to

    mMediaRouteSelector = new MediaRouteSelector.Builder()
                .addControlCategory( CastMediaControlIntent.categoryForCast(getString(R.string.cast_sdk_id)))
                .build();