4

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.

Community
  • 1
  • 1
Dirk R
  • 61
  • 4
  • Post the xml or programmatic creation of the button which is not being found. If it is a custom class, post its code. Post the *unfiltered* logcat of the run attempt. – Chris Stratton May 02 '14 at 19:14
  • So, are you saying that you have a layout xml that has the button there but findViewById() doesn't find it? Can it be that you have a similarly named xml under, say, layout-large-hdpi (or something else that is picked up by N7) that doesn't have the MediaRouteButton there? – Ali Naddaf May 02 '14 at 20:17
  • 1
    @AliNaddaf Christopher updated the second paragraph to detailing the folder structure. There is only one layout folder. – Dirk R May 02 '14 at 23:27
  • 1
    If you are getting a null for the findViewById(), then this is really an android question and not related to Chromecast. To see if your activity is actually picking up the layout xml that you expect, just make a small change to the layout and see if your N7 sees that change. If that doesn't help, you then need to create a test project that has the bare minimum files (removing all player stuff and cast stuff, etc) that exhibits your issue and put it somewhere that we can access to see what is happening. – Ali Naddaf May 03 '14 at 17:51
  • 1
    Thank you @AliNaddaf, we finally got it sorted out! – Dirk R May 21 '14 at 04:26

0 Answers0