The FIT Radio App uses a MediaRoute.Callback that is initialized in my CommandInterface class, that controls playback for the normal audio as well as the Chromecast audio. The NowPlayingActivity is initially set as the mMediaRouteButtonHandler inside of the MediaRoute.Callback, only if the MediaRouteButton exists.
The now_playing.xml is now in the default layout, with protective measures in the java source code. Our first measure of protection against null pointer exceptions (with the G2) was removing the the MediaRouteButton from the default layout. The second measure that was taken to stop the null pointer exceptions was to add the null check in the NowPlayingActivity.setVisibility(int visibility) code sample below for any other version that may not provide the Button (N7, etc.).
The MediaRouter callback is added in the NowPlayingActvity.onResume(), and removed in the NowPlayingActvity.onPause(). The following sample displays the visibility handling in the MediaRoute.Callback.onRouteAdded:
public void onRouteAdded(MediaRouter router, RouteInfo route) {
if(hasMediaRouteButtonHandler()) mMediaRouteButtonHandler.setVisibility(View.VISIBLE);
++mRouteCount;
}
I have access to a Google G2, a Note 3, a Samsung S III, HTC One M7, and a Nexus 7. After starting the MediaRoute.Callback, the MediaRoute Button shows on all devices [using NowPlayingActivity.setVisibility(int visibility)], except with the Nexus 7 and the Google G2.
public void setVisibility(int visibility) {
if(getMediaRouteButton() != null) getMediaRouteButton().setVisibility(visibility);
}
I am using the findViewById(int) method in the NowPlayingActvity and it returns null in the latter cases (Nexus 7 & G2), but works on the S III, HTC One, and Note 3.
public MediaRouteButton getMediaRouteButton() {
return (MediaRouteButton) findViewById(R.id.media_route_button);
}
I have looked on StackOverflow for similar questions, and there were no real answers for the issue:
With the first question that I found the user figured it out eventually, and posted that he had no clear solution.
android.support.v7.app.MediaRouteButton does not display
Another question was answered by the poster, and the solution came from using a real device instead of the emulator:
Why my MediaRouteButton not available to find any cast devices?
I have debugged this with all the above devices, with Dirk (My Contractee) over my shoulder! So this is a critical issue guys, and we really need your help!
Thanks in advance,
Christopher Miller, posting on behalf of Dirk @ Fit Radio.