1

I'm trying to use ShareActionProvider with actionbarsherlock, but i got:

java.lang.NullPointerException
at com.iuculano.tvitaliane.Start.onCreateOptionsMenu(Start.java:387)
at com.actionbarsherlock.app.SherlockActivity.onCreatePanelMenu(SherlockActivity.java:167
at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:542)

The relevant code on onCreateOptionsMenu(Menu menu) is this:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    super.onCreateOptionsMenu(menu);
    MenuItem menuItem = menu.findItem(R.id.share);

    //MenuInflater blowUp = getSupportMenuInflater();
    //getSupportMenuInflater().inflate(R.menu.menuhome, menu);


    ShareActionProvider mShareActionProvider =  (ShareActionProvider) menuItem.getActionProvider();  //line 387

    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.sharesubject));
    shareIntent.putExtra(Intent.EXTRA_SUBJECT,getString(R.string.sharetext));

    mShareActionProvider.setShareIntent(shareIntent);

and the menuhome.xml has this:

<item
    android:id="@+id/share"
    android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"
    android:enabled="true"
    android:showAsAction="ifRoom"
    android:visible="true">
</item>

Where I'm wrong?

Giuseppe
  • 2,093
  • 1
  • 21
  • 26
  • Are you sure ActionBar Sherlock supports ShareActionProvider? – Oscar Abraham May 18 '12 at 19:05
  • I think it supoorts ShareActionProvider: https://github.com/JakeWharton/ActionBarSherlock/blob/master/samples/demos/src/com/actionbarsherlock/sample/demos/ShareActionProviders.java – Giuseppe May 19 '12 at 15:59
  • Cool! That was two months ago. Maybe you don't have that in the library? I don't see an error in your code at first sight. I'll look into it; it might be one has to use something a little different when using ActionBar Sherlock, like it is with fragments. – Oscar Abraham May 20 '12 at 16:22

1 Answers1

7

Fixed... I need to inflate the menu before calling findItem

Giuseppe
  • 2,093
  • 1
  • 21
  • 26