我尝试使用CastCompanionLibrary为我的应用程序实现强制转换功能。当它尝试将 Media Router Cast Button 添加到 ActionBar 时,我收到此错误:
10-03 19:33:20.098 11781-11781/de.resper.e2cast W/MenuItemCompat﹕ getActionProvider: item does not implement SupportMenuItem; returning null
10-03 19:33:20.098 11781-11781/de.resper.e2cast W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41ebde18)
10-03 19:33:20.098 11781-11781/de.resper.e2cast E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: de.resper.e2cast, PID: 11781
java.lang.NullPointerException
at com.google.sample.castcompanionlibrary.cast.BaseCastManager.addMediaRouterButton(BaseCastManager.java:355)
at de.resper.e2cast.ChannelCastActivity.onCreateOptionsMenu(ChannelCastActivity.java:655)
CahnnelCastActivity 中的位置是:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
mCastManager.addMediaRouterButton(menu, R.id.media_route_menu_item); <--- ERROR
return true;
}
我已经包括:
import android.support.v7.app.ActionBarActivity;
并扩展为:
public class ChannelCastActivity extends ActionBarActivity
在 BaseCastManager 中是错误行:
public MenuItem addMediaRouterButton(Menu menu, int menuResourceId) {
MenuItem mediaRouteMenuItem = menu.findItem(menuResourceId);
MediaRouteActionProvider mediaRouteActionProvider = (MediaRouteActionProvider) MenuItemCompat.getActionProvider(mediaRouteMenuItem);
mediaRouteActionProvider.setRouteSelector(mMediaRouteSelector); <--- ERROR
if (null != getMediaRouteDialogFactory()) {
mediaRouteActionProvider.setDialogFactory(getMediaRouteDialogFactory());
}
return mediaRouteMenuItem;
}
在 AndroidManifest.xml 中:
<activity
android:name=".ChannelCastActivity"
android:label="@string/title_activity_channel_cast"
android:theme="@style/Theme.AppCompat.Light"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="de.resper.e2cast.MainActivity" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</activity>