这是运行时异常
java.lang.ClassCastException:android.support.v7.widget.ShareActionProvider无法强制转换为android.view.View
我尝试了很多不同的方法,但我知道它与ShareActionProvider有关我可以添加链接到网站,其中有几种方法可以做到这一点,但它在这里不起作用是整个代码
通过“this”我的意思是实例化ShareActionProvider
请不要将其设置为重复,不是我已经查看了SO而且他们都没有帮助... Sod to say
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// TODO: Implement this method
MenuInflater inflator = getMenuInflater();
inflator.inflate(R.menu.sidebar_menu, menu);
SubMenu subMenu = menu.addSubMenu("Themes");
subMenu.add(0 , blue , 0 , "Blue");
subMenu.add(0, pink , 1, "Pink");
items = subMenu.getItem().getItemId();
// tool bar menu
MenuItem mi = menu.findItem(R.id.searchable);
mi.setIcon(android.R.drawable.ic_search_category_default);
SearchView searchView = (SearchView)MenuItemCompat.getActionView(mi);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
sap = (ShareActionProvider) MenuItemCompat.getActionProvider(menu.findItem(R.id.share));
Intent intentShare = new Intent(Intent.ACTION_SEND);
intentShare.setType("text/plain");
intentShare.putExtra(Intent.EXTRA_SUBJECT ,"Subject here");
intentShare.putExtra(Intent.EXTRA_TEXT , "Here is the share content body");
startActivity(Intent.createChooser(intentShare,"Share via"));
sap.setShareIntent(intentShare);
MenuItemCompat.OnActionExpandListener el = new MenuItemCompat.OnActionExpandListener(){
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when action item collapses
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
return true; // Return true to expand action view
}};
MenuItem actionMenuItem = menu.findItem(R.id.searchable);
// Assign the listener to that action item
MenuItemCompat.setOnActionExpandListener(actionMenuItem,el);
return super.onCreateOptionsMenu(menu);
}
任何人都可以帮忙!我想我已经清楚了解这个问题,如果需要,我可以发布menu.XML文件以及appmanifest.XML以及其他我认为没有理由提出的XML文件...谢谢
这是所要求的menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/searchable"
android:title="@string/tbmenuitem_search"
myapp:showAsAction="ifRoom|collapseActionView"
myapp:actionViewClass="android.support.v7.widget.SearchView"
/>
<item
android:id="@+id/share"
android:showAsAction="ifRoom"
android:title="@string/sharetitle"
myapp:actionViewClass="android.support.v7.widget.ShareActionProvider"
/>
<item
android:id="@+id/playlist"
android:title="Play lists"
/>
<item
android:id="@+id/muteoption"
android:title="Mute"
/>
<item
android:id="@+id/unmuteoption"
android:title="UnMute"
/>
<item
android:id="@+id/sharewidget"
android:title="Share"
/>
[编辑]我的XML文件actionViewClass应该是actionViewProvider因此运行时错误。