我正在尝试在我的 ActionBarSherlock 中实现 ShareActionProvider。
以下代码给了我这个错误:
无法从 ActionProvider 转换为 ShareActionProvider
阅读活动.java
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu2b, (Menu) menu);
menu.getItem(0).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS|MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.getItem(1).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS|MenuItem.SHOW_AS_ACTION_WITH_TEXT);
MenuItem menuItem = menu.findItem(R.id.share);
// Get the provider and hold onto it to set/change the share intent.
ShareActionProvider shareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
// Set the default share intent
shareActionProvider.setShareIntent(getDefaultShareIntent());
return super.onCreateOptionsMenu(menu);
}
菜单2b.xml
<?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_share_action_bar"
android:actionProviderClass="android.widget.ShareActionProvider"/>
</menu>