fixes NPE from #1974

This commit is contained in:
Domingos Lopes 2016-06-03 16:44:19 -04:00
parent 405630ee50
commit b46b2247b4
2 changed files with 22 additions and 6 deletions

View File

@ -67,9 +67,16 @@ public abstract class CastEnabledActivity extends AppCompatActivity
@CallSuper
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
mediaRouteActionProvider = castManager
.addMediaRouterButton(menu.findItem(R.id.media_route_menu_item));
MenuItem mediaRouteButton = menu.findItem(R.id.media_route_menu_item);
if (mediaRouteButton == null) {
Log.wtf(TAG, "MediaRoute item could not be found on the menu!");
mediaRouteActionProvider = null;
return true;
}
mediaRouteActionProvider = castManager.addMediaRouterButton(mediaRouteButton);
if (mediaRouteActionProvider != null) {
mediaRouteActionProvider.setEnabled(castButtonVisibilityManager.shouldEnable());
}
return true;
}

View File

@ -24,6 +24,8 @@ package de.danoeh.antennapod.core.cast;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.view.ActionProvider;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.media.MediaRouter;
import android.util.Log;
@ -1739,9 +1741,16 @@ public class CastManager extends BaseCastManager implements OnFailedListener {
*
* @param menuItem MenuItem of the Media Router cast button.
*/
public final SwitchableMediaRouteActionProvider addMediaRouterButton(MenuItem menuItem) {
SwitchableMediaRouteActionProvider mediaRouteActionProvider = (SwitchableMediaRouteActionProvider)
MenuItemCompat.getActionProvider(menuItem);
public final SwitchableMediaRouteActionProvider addMediaRouterButton(@NonNull MenuItem menuItem) {
ActionProvider actionProvider = MenuItemCompat.getActionProvider(menuItem);
if (!(actionProvider instanceof SwitchableMediaRouteActionProvider)) {
Log.wtf(TAG, "MenuItem provided to addMediaRouterButton() is not compatible with " +
"SwitchableMediaRouteActionProvider." +
((actionProvider == null) ? " Its action provider is null!" : ""));
return null;
}
SwitchableMediaRouteActionProvider mediaRouteActionProvider =
(SwitchableMediaRouteActionProvider) actionProvider;
mediaRouteActionProvider.setRouteSelector(mMediaRouteSelector);
if (mCastConfiguration.getMediaRouteDialogFactory() != null) {
mediaRouteActionProvider.setDialogFactory(mCastConfiguration.getMediaRouteDialogFactory());