2

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.

Community
  • 1
  • 1
David Corsalini
  • 7,958
  • 8
  • 41
  • 66
  • 1
    Ok, let's walk through the information provided: "at com.corsalini.david.calcolopesi.ActCalcolo.onCreateOptionsMenu(ActCalcolo.java:162)" What is on line 162 in ActCalcolo.java? – Sam Jul 25 '12 at 16:05
  • mShareActionProvider.setShareIntent(shareIntent); //Line 162 I'm sorry =) – David Corsalini Jul 25 '12 at 16:07
  • I have never used ActionBarSherlock, but `mShareActionProvider` is null so this `menuItem.getActionProvider();` doesn't find anything (it simply returns null). Next we'll check the documentation... – Sam Jul 25 '12 at 16:09
  • I don't think i understood you answer. menuItem.getActionProvider() take the menuitem from the line above and the item is there for sure. Or am I missing something? – David Corsalini Jul 25 '12 at 16:13
  • Well, the NPE is in line 162. The only variable that could cause this is `mShareActionProvider`, so when you initialized `mShareActionProvider` something went awry. `menuItem` is fine (no NPE here) so something about getActionProvider() and setting your ActionProvider in `miShare` is off. (But I not finding much documentation online... Still looking) – Sam Jul 25 '12 at 16:20
  • Just to be certain is `main_menu.xml` the Menu XML you posted? – Sam Jul 25 '12 at 16:29
  • Don't know why my last comment didn't show up, but yes, menu_main is the xml in the question. – David Corsalini Jul 25 '12 at 18:03

1 Answers1

6

Notes

I have tried to duplicate your error and the only two ways I can cause mShareActionProvider to be null is to:

  1. leave this out entirely: android:actionProviderClass=...
  2. use the Android class: android:actionProviderClass="android.widget.ShareActionProvider"

As you discovered in EDIT 2 the appropriate ActionBarSherlock code is:

android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"

Suggestions

1) ... If I try to put (the menu item) on top of the list, the app crashes and gives the same logcat

Try creating a new menu XML file, save this as res/menu/temp_menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/miShare"
        android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"
        android:showAsAction="always"
        android:title="Share" />
</menu>

And load it with:

inflater.inflate(R.menu.temp_menu, menu);

2) If "share" is in the overflow menu nothing happens when I select it.

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:

mShareActionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);

You said:

And now the NullPointerException is here. Is it possible that there is no such file and so I get the exception?

The reason the NPE moved here is because mSharedActionProvider is still null. You cannot reference the concept of null.method() or in your case null.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:

ShareActionProvider mShareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
Log.w("NullPointerException", "Is mShareActionProvider null: " + (mShareActionProvider == null));

Hope that helps!

Sam
  • 86,580
  • 20
  • 181
  • 179
  • I cannot find this method, SupporteED is a bit strange and neither this or Support works. Plus, [here](https://github.com/JakeWharton/ActionBarSherlock/issues/527) the creator of ABS doesn't speak about getSupportedActionProvider() when he answer the question. – David Corsalini Jul 26 '12 at 07:48
  • I don't think getSupportActionProvider exists, I get an error when i write it down, Eclipse doesn't find it with the autocompletition and i can't find it on the internet. But the explanation on the nullpointerexception actually clarified the problem. – David Corsalini Jul 26 '12 at 19:42
  • ' /** * Gets the {@link ActionProvider}. * * return The action provider. * * see ActionProvider * see #setActionProvider(ActionProvider) */ public ActionProvider getActionProvider();' This is the method in com.actionbarsherlock.view.MenuItem. – David Corsalini Jul 26 '12 at 19:56
  • I laughed at myself yesterday when I realized that I was reading what I _wanted_ to read, _not_ what was written. You're right: there is no such method as getSupportActionProvider(), my apologies. I installed ActionBarSherlock to try to solve this, I update my answer. – Sam Jul 27 '12 at 20:20
  • Following your suggestion i created a menu with only the shareprovider item, the activity doesn't crash, but if i click on the button nothing happens. I also get a very long list of method that didn't resolve from ABS. Here is a snippet from the log: I/dalvikvm(564): Could not find method android.widget.LinearLayout.setTranslationX, referenced from method com.actionbarsherlock.internal.nineoldandroids.widget.NineLinearLayout.setTranslationX -W/dalvikvm(564): VFY: unable to resolve virtual method 3511: Landroid/widget/LinearLayout;.setTranslationX (F)V I'll give up soon on this button =( – David Corsalini Jul 28 '12 at 20:13
  • Do you have the latest 4.1.0 version? – Sam Jul 28 '12 at 21:55
  • Yes, downloaded just a few days ago! – David Corsalini Jul 29 '12 at 01:08
  • 1
    @DavidCorsalini hi mate, have u solved this issue? I met the same issue and can't find out the reason. thx – dumbfingers May 23 '13 at 21:44