Android 使用CCL(CastCompanionLibrary)进行ChromeCast时,无法在MediaRouteButton中获取活动

Android 使用CCL(CastCompanionLibrary)进行ChromeCast时,无法在MediaRouteButton中获取活动,android,chromecast,google-cast,Android,Chromecast,Google Cast,目前,我正在开发ChromeCast的例子CastVideos和android CCLCastCompanyLibrary 我尝试使用与CastVideos android相同的编码流程,但当我单击ActionBar上的CastButton时,出现了一个null指针异常getActivity并返回null 我自己搭建了v7 mediarouter,添加了一些日志供观察 MediaRouteButton.java中修改的代码 private Activity getActivity() {

目前,我正在开发ChromeCast的例子CastVideos和android

CCLCastCompanyLibrary

我尝试使用与CastVideos android相同的编码流程,但当我单击ActionBar上的CastButton时,出现了一个null指针异常getActivity并返回null

我自己搭建了v7 mediarouter,添加了一些日志供观察

MediaRouteButton.java中修改的代码

private Activity getActivity() {
    // Gross way of unwrapping the Activity so we can get the FragmentManager
    Context context = getContext();
    
    if (context instanceof ContextWrapper) {
        Log.d(TAG, "[getActivity] context instanceof ContextWrapper");
    } else if (context instanceof Activity) {
        Log.d(TAG, "[getActivity] context instanceof Activity");
    }
    while (context instanceof ContextWrapper) {
        if (context instanceof Activity) {
            return (Activity)context;
        }
        context = ((ContextWrapper)context).getBaseContext();
        Log.d(TAG, "[getActivity] getBaseContext = " + context);
    }
    Log.d(TAG, "[getActivity] return null");
    return null;
}
日志显示:

1.以android为例 03-09 15:35:33.473:D/MediaRouteButton11409:[getActivity]context=android.view。ContextThemeWrapper@ae4badf

03-09 15:35:33.473:D/MediaRouteButton11409:[getActivity]ContextWrapper的上下文实例

03-09 15:35:33.473:D/MediaRouteButton11409:[getActivity]getBaseContext=android.view。ContextThemeWrapper@3bcdb32c

03-09 15:35:33.473:D/MediaRouteButton11409:[getActivity]getBaseContext=com.google.sample.cast.refplayer。VideoBrowserActivity@3a39dcf1

MediaRouteButton可以成功获取活动

2.对于我自己的应用程序: 03-09 15:23:46.453:D/MediaRouteButton7272:[getActivity]context=android.view。ContextThemeWrapper@eb4848

03-09 15:23:46.453:D/MediaRouteButton7272:[getActivity]ContextWrapper的上下文实例

03-09 15:23:46.453:D/MediaRouteButton7272:[getActivity]getBaseContext=com.mychromecast.application@1a3e8984

03-09 15:23:46.453:D/MediaRouteButton7272:[getActivity]getBaseContext=android.app。ContextImpl@3e3e6e1

03-09 15:23:46.453:D/MediaRouteButton7272:[getActivity]返回null

当尝试获取BaseContext而不是像示例中那样获取ContextThemeWrapper和当前活动时,它得到了我定制的应用程序类和ContextImpl

因此,它无法正确地获取一个活动来处理FragmentManager等


需要您的帮助和提前感谢

您的活动继承自何处?我修改了v7 mediarouter的源代码,使其能够访问活动,而不是ActionBarActivity或FragmentActivity。因此,我的应用程序的活动继承了android.App.activity.抱歉,因为您没有使用标准版本的库,所以我无法查看正在发生的情况以提供任何有用的反馈。一般来说,修改这样的库并在生产中使用它是一个非常糟糕的主意,但我相信您已经意识到了风险,等等。是的,我知道这有点风险。但我的应用程序的活动继承了一些第三方定制的活动,我无法将其修改为使用actionbaractivity。对于这种情况有什么建议吗?由于存在代码结构,超级活动不允许使用actionbarActivity或fragmentActivity。。。?再次感谢!我唯一的建议是在库的修改版本中,在运行时真正跟踪和调试它,并查看它失败的原因。