Giuseppe
Giuseppe

Reputation: 2103

NullPointerException using ShareActionProvider + actionbarsherlock

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?

Upvotes: 1

Views: 2717

Answers (1)

Giuseppe
Giuseppe

Reputation: 2103

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

Upvotes: 7

Related Questions