我已经完成了几乎Chromecast集成,但我需要在活动的选项菜单中添加自定义强制转换按钮。怎么做??在菜单中添加自定义转换按钮作为项目时,我得到空指针异常。
我的代码部分是:
public void onCreate(Bundle savedInstanceState) {
mMediaRouter = MediaRouter.getInstance(getApplicationContext());
mMediaRouteSelector = new MediaRouteSelector.Builder()
.addControlCategory(CastMediaControlIntent.categoryForCast(APP_ID)).build();
mMediaRouteButton = (MediaRouteButton) findViewById(R.id.media_route_button);
mMediaRouterCallback = new MediaRouterCallback();
mCastListener = new CastListener();
mConnectionCallbacks = new ConnectionCallbacks();
mConnectionFailedListener = new ConnectionFailedListener();
// Set the MediaRouteButton selector for device discovery.
mMediaRouteButton.setRouteSelector(mMediaRouteSelector);
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/share"
android:title="Share"
android:icon="@drawable/ic_menu_share" />
<item android:id="@+id/feedback"
android:title="Feedback"
android:icon="@drawable/ic_menu_feedback" />
<item android:id="@+id/quit"
android:title="Quit"
android:icon="@drawable/ic_menu_close_clear_cancel" />
<android.support.v7.app.MediaRouteButton
android:id="@+id/media_route_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:mediaRouteTypes="user"
android:visibility="gone" />
</menu>
答案 0 :(得分:0)
对于MediaRouteButton,您的活动需要扩展FragmentActivity