I'm trying to use ActionBarSherlock + ShareActionProvider in a SherlockActivity. I've already checked this ( NullPointerException using ShareActionProvider + actionbarsherlock) but it doesn't solve my problem. I think the code is correct, but I keep getting FC and this logcat:
07-25 15:31:26.758: E/AndroidRuntime(911): FATAL EXCEPTION: main
07-25 15:31:26.758: E/AndroidRuntime(911): java.lang.NullPointerException
07-25 15:31:26.758: E/AndroidRuntime(911): at com.corsalini.david.calcolopesi.ActCalcolo.onCreateOptionsMenu(ActCalcolo.java:162)
07-25 15:31:26.758: E/AndroidRuntime(911): at com.actionbarsherlock.app.SherlockActivity.onCreatePanelMenu(SherlockActivity.java:173)
07-25 15:31:26.758: E/AndroidRuntime(911): at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:556)
07-25 15:31:26.758: E/AndroidRuntime(911): at com.actionbarsherlock.internal.ActionBarSherlockCompat.preparePanel(ActionBarSherlockCompat.java:483)
07-25 15:31:26.758: E/AndroidRuntime(911): at com.actionbarsherlock.internal.ActionBarSherlockCompat.dispatchInvalidateOptionsMenu(ActionBarSherlockCompat.java:273)
07-25 15:31:26.758: E/AndroidRuntime(911): at com.actionbarsherlock.internal.ActionBarSherlockCompat$1.run(ActionBarSherlockCompat.java:988)
07-25 15:31:26.758: E/AndroidRuntime(911): at android.os.Handler.handleCallback(Handler.java:587)
07-25 15:31:26.758: E/AndroidRuntime(911): at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 15:31:26.758: E/AndroidRuntime(911): at android.os.Looper.loop(Looper.java:123)
07-25 15:31:26.758: E/AndroidRuntime(911): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-25 15:31:26.758: E/AndroidRuntime(911): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 15:31:26.758: E/AndroidRuntime(911): at java.lang.reflect.Method.invoke(Method.java:507)
07-25 15:31:26.758: E/AndroidRuntime(911): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-25 15:31:26.758: E/AndroidRuntime(911): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-25 15:31:26.758: E/AndroidRuntime(911): at dalvik.system.NativeStart.main(Native Method)
Here is the code:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.miShare);
ShareActionProvider mShareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
// Set the default share intent
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_text));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share_subject));
mShareActionProvider.setShareIntent(shareIntent); //Line 162
return true;
}
And here is the xml for the menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/miPref"
android:icon="@drawable/ic_action_settings"
android:showAsAction="ifRoom"
android:title="@string/settings"
android:visible="true" />
<item
android:id="@+id/miReset"
android:icon="@drawable/ic_action_reset"
android:showAsAction="ifRoom"
android:title="@string/reset"
android:visible="true" />
<item
android:id="@+id/miShare"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="ifRoom"
android:title="@string/share" />
</menu>
EDIT 1:
I'm trying to add this line: mShareActionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
And now the NullPointerException is here. Is it possible that there is no such file and so i get the exception?
EDIT 2: Do not take care of EDIT 1 I've found that the problem was in the XML. You have to use:
android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"
but i was still using the original android widget. I have also included:
menuItem.setActionProvider(mShareActionProvider);
in the activity, but i think it's redundant.
Now i've got two problems: 1. the app doesn't crash only if the "share" button doesn't show up in the action bar, if i try to put it on top of the list, the app crash and give the same logcat as the one posted here. 2. When i click "share" nothing happens.
mShareActionProvider
is null so thismenuItem.getActionProvider();
doesn't find anything (it simply returns null). Next we'll check the documentation... – CrankmShareActionProvider
, so when you initializedmShareActionProvider
something went awry.menuItem
is fine (no NPE here) so something about getActionProvider() and setting your ActionProvider inmiShare
is off. (But I not finding much documentation online... Still looking) – Crankmain_menu.xml
the Menu XML you posted? – Crank