我正在为chromecast开发用于Chromecast的钛安卓模块我试图创建一个菜单栏。 我正在使用以下代码创建菜单:
protected Menu newMenuInstance(Context context) {
try {
Class <? > menuBuilderClass = Class.forName("com.android.internal.view.menu.MenuBuilder");
Constructor <? > constructor = menuBuilderClass.getDeclaredConstructor(Context.class);
return (Menu) constructor.newInstance(context);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
现在,我正在通过以下代码扩充菜单栏:
@Kroll.method
public void castButtonLoader() {
Menu menu = newMenuInstance(TiApplication.getAppCurrentActivity());
Log.i("menu ", "Menu instance created " + menu);
int menuId = TiApplication.getAppRootOrCurrentActivity()
.getResources()
.getIdentifier("main", "menu",
TiApplication.getInstance().getPackageName());
Log.i("menu ID", "Menu id get =" + menuId);
MenuInflater inflater = TiApplication.getAppCurrentActivity()
.getMenuInflater();
inflater.inflate(menuId, menu);
MenuItem mediaRouteMenuItem;
mediaRouteMenuItem = menu.findItem(menuId);
Log.i("MediaRoute ", " Media route menu item ref =" + mediaRouteMenuItem);
MediaRouteActionProvider mediaRouteActionProvider = (MediaRouteActionProvider) MenuItemCompat.getActionProvider(mediaRouteMenuItem);
Log.i("MediaRouteActionProvider ", " MediaRouteActionProvider ref =" + mediaRouteActionProvider);
// Set the MediaRouteActionProvider selector for device discovery.
mediaRouteActionProvider.setRouteSelector(mMediaRouteSelector);
}
现在,在MenuItem上,它返回null。任何形式的帮助表示赞赏。 感谢。
答案 0 :(得分:0)
您可能需要使用AppCompat库中的MenuBuilder,因为这也是Titanium使用的。你试过吗?