I am trying to create a share button for my app but every time I try to run the app stops working. Specifically, it stops working when I try to create the intent in the OnCreateOptionsMenu() method. Anyone know why this is? Thank you in advance!
Here is my code:
MainActivity.java
@Override public boolean onCreateOptionsMenu (Menu menu){ /** Inflating the current activity's menu with res/menu/items.xml */ getMenuInflater().inflate(R.menu.menu_main, menu); /** Getting the action provider associated with the menu item whose id is share */ ShareActionProvider mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.action_share).getActionProvider(); /** Setting a share intent */ mShareActionProvider.setShareIntent(getDefaultShareIntent()); return super.onCreateOptionsMenu(menu); } /** Returns a share intent */ private Intent getDefaultShareIntent(){ Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT"); intent.putExtra(Intent.EXTRA_TEXT,"Extra Text"); return intent; }
menu_main.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=".MainActivity"> <item android:id="@+id/action_about" android:orderInCategory="10" app:showAsAction="never" android:title="About" /> <item android:id="@+id/action_settings" android:orderInCategory="100" app:showAsAction="never" android:title="EDA Information" /> <item android:id="@+id/action_share" app:showAsAction="ifRoom" android:actionProviderClass="android.widget.ShareActionProvider" android:title="Share"/>
No comments:
Post a Comment