Cannot cast from ActionProvider to ShareActionProvider - java

I am trying to implement a ShareActionProvider in my ActionBarSherlock.
The following code gives me this error :
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>

You should be using the ShareActionProvider from ABS, not the stock one.
android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"

Related

Show popup menu from an action bar item

So, I am trying to add a PopupMenu to a ActionBar item that sorts the list. I have seen some answers in stackoverflow but none of them worked for me. Below is the code.
XML
Menu Item:
<item
android:id="#+id/sortByBackers"
android:icon="#drawable/ic_sort"
android:title="Sort By Backers"
app:showAsAction="always" />
PopupMenu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/default_list"
android:title="Default" />
<item
android:id="#+id/sort"
android:title="Sort" />
</menu>
Activity
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.bar_menu, menu);
//...
//...
MenuItem sortItem = menu.findItem(R.id.sortByBackers);
sortItem.setOnMenuItemClickListener(item -> {
View view = findViewById(R.id.default_list);
popupMenu = new PopupMenu(getApplicationContext(), view);
popupMenu.inflate(R.menu.sort_or_default);
popupMenu.setOnMenuItemClickListener(item1 -> {
switch (item1.getItemId()) {
case R.id.default_list:
kickstarterAdapter.setData(kickstartersList);
break;
case R.id.sort:
kickstarterAdapter.sortList();
break;
default:
kickstarterAdapter.sortList();
}
popupMenu.show();
return true;
});
return true;
});
`

Items in menu not showing action bar, rather being displayed without any icons in three dots

Previously my app name was being displayed hence I had to use getSupportActionBar().setDisplayShowTitleEnabled(false) to remove the text, but now in its place a blank action is displayed with three dots on the side showing the list items that I have defined.
I want Refresh and Back actions to be displayed directly into the action bar with their respective icons
eventdetails.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_refresh"
app:showAsAction="always"
android:title="Refresh"
android:icon="#drawable/ic_action_refresh"
/>
<item
android:id="#+id/action_settings"
android:title="Settings"
app:showAsAction="ifRoom"
>
</item>
<item
android:id="#+id/action_back"
android:title="Back"
android:icon="#drawable/ic_action_back"
app:showAsAction="always"
/>
</menu>
EventDetails.java
public class EventDetails extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarEventDetails);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.eventdetails, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_back:
Intent action_back = new Intent(EventDetails.this, EventView.class);
startActivity(action_back);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Your xmlns:app is not setup properly
change:
xmlns:app="schemas.android.com/apk/res-auto"
to
xmlns:app="http://schemas.android.com/apk/res-auto"

My menu_main.xml do not show on my main_activity

I just a newbie in android programming, i am building a new app from a tutorial.
I have make the main menu like this
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="id.go.kemenkeu.itjen_kemenkeu.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
app:showAsAction="never"
android:title="Settings" />
<item
android:id="#+id/navigate"
android:title="#string/next"
android:orderInCategory="200"
app:showAsAction="always"
android:icon="#drawable/ic_next"/> </menu>
and the MainActivity.Java like this
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar= (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if(id==R.id.action_settings){
Toast.makeText(this,"Hi, Anda Baru Saja Menekan"+item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
if(id==R.id.navigate){
startActivity(new Intent(this, SubActivity.class));
}
return super.onOptionsItemSelected(item);
} }
I call the +id/navigate in MainActivity.Java, but when i run in emulator, the style inside +id/navigate didn't show. What is wrong here?
you have to override createoptionmenu for intialize your own menu in toolbar.
try adding this in you activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mymenu_main, menu);
return super.onCreateOptionsMenu(menu);
}
without onCreateOptionsMenu you cant show menu. so add onCreateOptionsMenu to your Activity Class like,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
Happy Coding!!!
You must inflate the option menu
Add this line.
getMenuInflater().inflate(R.menu.main, menu);
You have to override the onCreateOptionsMenu in you Activity and inflate the menu and check it
more details you have to check in Developer Site

ActionProvider cannot be converted to ShareActionProvider

import android.widget.ShareActionProvider;
...
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.detailfragment, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
ShareActionProvider mShareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(menuItem);
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(createShareForecastIntent());
} else {
Log.d(LOG_TAG, "Share Action Provider is null?");
}
}
This code shows the following error:
incompatible types: ActionProvider cannot be converted to ShareActionProvider
The following is the detailfragment.xml file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_share"
android:title="#string/action_share"
app:showAsAction="always"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
Please help.
android.widget.ShareActionProvider and android.support.v7.widget.ShareActionProvider are not the same class. Change your import to match your actionProviderClass.
it worked for me, give a try.
import android.support.v7.widget.ShareActionProvider
remove it and add this import:
import android.widget.ShareActionProvider;

Menu item not added to actionbar

I have looked for a solution to the problem on Stack Overflow and google, but nothing seems to help. To clarify, I am talking about the one with the id "favourite"
Here is my menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.rasmase.smbcbrowser.MainActivity" >
<item android:id="#+id/favourite"
android:icon="#drawable/ic_favourite"
android:title="#string/favourite"
app:showAsAction="ifRoom" />
<item
android:id="#+id/go_to_comic"
android:orderInCategory="100"
android:title="#string/Go_to_comic"
app:showAsAction="never"/>
And my onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
Anyone else have any idea what is wrong?
EDIT:
Turns out that I am retarded. The Icon was a black star on a black actionbar. Image for reference: http://i.stack.imgur.com/tVWm7.png

Categories