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.
Notes
I have tried to duplicate your error and the only two ways I can cause
mShareActionProvider
to benull
is to:android:actionProviderClass=...
android:actionProviderClass="android.widget.ShareActionProvider"
As you discovered in EDIT 2 the appropriate ActionBarSherlock code is:
Suggestions
Try creating a new menu XML file, save this as res/menu/temp_menu.xml:
And load it with:
This is an ActionBarSherlock backwards compatibility quirk detailed here, Issue #455.
Null Pointer Exception
From a few of your comments, I want to clarify what exactly a NullPointerException is. When you tried adding this line:
You said:
The reason the NPE moved here is because
mSharedActionProvider
is still null. You cannot reference the concept ofnull.method()
or in your casenull.setShareHistoryFileName()
, this is what a NPE is. In other words, you cannot ask nothing to do something. It doesn't matter here whether the file exists or not, the app crashes before this point.You can prove this for yourself in the debugger or by adding a line like this:
Hope that helps!