我试图在我的ActionBarSherlock中实现一个ShareActionProvider.
Cannot cast from ActionProvider to ShareActionProvider
ReadingActivity.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); }
menu2b.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>
解决方法
你应该使用来自ABS的ShareActionProvider,而不是股票.
android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"