目前我正在通过示例CastVideos-android和
开发ChromeCastCCL(CastCompanionLibrary)。
我尝试使用与CastVideos-android相同的编码流程,但是当我单击ActionBar上的CastButton时,有一个空指针异常(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;
}
日志显示:
03-09 15:35:33.473:D / MediaRouteButton(11409):[getActivity] context = android.view.ContextThemeWrapper@ae4badf
03-09 15:35:33.473:D / MediaRouteButton(11409):[getActivity] ContextWrapper的上下文实例
03-09 15:35:33.473:D / MediaRouteButton(11409):[getActivity] getBaseContext = android.view.ContextThemeWrapper@3bcdb32c
03-09 15:35:33.473:D / MediaRouteButton(11409):[getActivity] getBaseContext =com.google.sample.cast.refplayer.VideoBrowserActivity@3a39dcf1
MediaRouteButton可以成功获取活动。
03-09 15:23:46.453:D / MediaRouteButton(7272):[getActivity] context = android.view.ContextThemeWrapper@eb4848
03-09 15:23:46.453:D / MediaRouteButton(7272):[getActivity] ContextWrapper的上下文实例
03-09 15:23:46.453:D / MediaRouteButton(7272):[getActivity] getBaseContext = com.mychromecast.application @ 1a3e8984
03-09 15:23:46.453:D / MediaRouteButton(7272):[getActivity] getBaseContext = android.app.ContextImpl@3e3e6e1
03-09 15:23:46.453:D / MediaRouteButton(7272):[getActivity]返回null
在尝试使用getBaseContext()而不是ContextThemeWrapper和当前活动时,它获得了我的自定义应用程序类和ContextImpl。
因此,它无法正确处理FragmentManager等活动。
需要你的帮助,并提前致谢!