This repository has been archived by the owner. It is now read-only.

NullPointerException using ShareActionProvider #527

Closed
Derevko opened this Issue Jun 26, 2012 · 2 comments

Comments

Projects
None yet
3 participants
@Derevko

Derevko commented Jun 26, 2012

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>
@iNoles

This comment has been minimized.

Show comment Hide comment
@iNoles

iNoles Jun 27, 2012

It should be

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    getSupportMenuInflater().inflate(R.menu.menuhome, menu);
    MenuItem menuItem = menu.findItem(R.id.menu_share);

    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);
}

your menu file didn't have title and icon too.

iNoles commented Jun 27, 2012

It should be

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    getSupportMenuInflater().inflate(R.menu.menuhome, menu);
    MenuItem menuItem = menu.findItem(R.id.menu_share);

    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);
}

your menu file didn't have title and icon too.

@JakeWharton

This comment has been minimized.

Show comment Hide comment
@JakeWharton

JakeWharton Jun 27, 2012

Owner

The key part to take away is that you need to inflate the menu before calling findItem. You don't need an icon but a title is required if the item were to be pushed into the overflow menu.

Owner

JakeWharton commented Jun 27, 2012

The key part to take away is that you need to inflate the menu before calling findItem. You don't need an icon but a title is required if the item were to be pushed into the overflow menu.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.