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
the variable: Android.Support.V7.Widget.ShareActionProvider shareActionProvider;
this.MenuInflater.Inflate(Resource.Menu.share_action_provider, menu);
var shareItem = menu.FindItem(Resource.Id.menu_item_share_action_provider_action_bar);
MenuItemCompat.SetShowAsAction (shareItem, MenuItemCompat.ShowAsActionIfRoom);
var actionprov = new Android.Support.V7.Widget.ShareActionProvider (this);
MenuItemCompat.SetActionProvider (shareItem, actionprov);
var test = MenuItemCompat.GetActionProvider (shareItem);
shareActionProvider = test.JavaCast<Android.Support.V7.Widget.ShareActionProvider>();
var intent = new Intent(Intent.ActionSend);
intent.SetType("text/plain");
intent.PutExtra(Intent.ExtraText, "ActionBarCompat is Awesome! Support Lib v7 #Xamarin");
shareActionProvider.SetShareIntent (intent);
return base.OnCreateOptionsMenu(menu);
this did the trick with me... i just created my own shareactionprovider ! and i set it myself, then get it... and maybe theres some code even thats not needed.. but its a lot of casting AND MAKE SURE you use the right ones all the time, if you just type "ShareActionProvider" you are in fact using V4..! instead of V7
In my case it was wrong namespace in menu.xml
:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu_item_share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
Pay attention to app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
: it should have
Unfortunatelly, the compiler compiles it without errors, only Android Studio makes notification with underlining.
For Android Pie the action provider class has changed again:
app:actionProviderClass="androidx.appcompat.widget.ShareActionProvider"
I had the same nullPointer probleme in my app. As @josh527 stated, I forgot to define a custom xml namespace for my application. I know it was not your probleme, but some people may reach your post like me and not see that so I just wanted to spot it ;)
Make sure that your class extends AppCompatActivity instead of just Activity.
Note: Edited to reflect the updated app compat library.
After some reading and that includes probably some of you responses, finally this issued is solved:
Share_Menu.xml. Make sure you have a custom namespace and the actionProvider class is from that custom namespace as well as the correct value: android.support.v7.widget.ShareActionProvider
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_share"
android:title="@string/action_detail_share"
myapp:showAsAction="always"
myapp:actionProviderClass="android.support.v7.widget.ShareActionProvider"></item>
</menu>
Detail_Activity.java
2.1. Inherit from ActionBarActivity instead of Activity
2.2. Add the correct imports
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
AndroidManifest.xml Add the android:theme="@style/Theme.AppCompat.Light"
<activity
android:name=".detail_activity"
android:label="@string/title_activity_detail_activity"
android:theme="@style/Theme.AppCompat.Light"
android:parentActivityName=".main_activity" >
Build.gradle
4.1. In my case just to stay in the safe side I turn offf ProGuard in Debug.
debug {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
4.2. Make sure that you have the following compile section in dependencies
`compile 'com.android.support:appcompat-v7:20.0.+'`