I tried to use android.support.v7.widget.ShareActionProvider on actionbar in my app. So I followed the example from android document but got some issues.
Here\'s my m
Here this is the only solution that works to make ShareActionProvider not null...i use set ActionProvider instead...see the code below:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.messages_activity_menu, menu);
MenuItem menuItem = menu.findItem(R.id.menu_item_share);
shareActionProvider = new ShareActionProvider(this);
MenuItemCompat.setActionProvider(menuItem, shareActionProvider);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.menu_item_share){
onShareAction();
}
return super.onOptionsItemSelected(item);
}
private void onShareAction(){
// Create the share Intent
String playStoreLink = "https://play.google.com/store/apps/details?id=" + getPackageName();
String yourShareText = getResources().getString(R.string.share_text) + playStoreLink;
Intent shareIntent = ShareCompat.IntentBuilder.from(this).setType("text/plain").setText(yourShareText).getIntent();
// Set the share Intent
if (shareActionProvider != null) {
shareActionProvider.setShareIntent(shareIntent);
}
}
and...xml
and other things that may be checked:
the activity has to extends ActionBarActivity:
MyActivity extends ActionBarActivity
check and use this imports:
import android.support.v4.app.ShareCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.OnNavigationListener;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
In AndroidManifest.xml put this line in your activity's tag attributes:
android:theme="@style/Theme.AppCompat.Light"
If you dont know how to import v7 and v4 compatibility libraries see: http://developer.android.com/tools/support-library/setup.html