0

I'm following a course on Udacity on building an Android app (weather, in this case). I've been having trouble implementing a Share action. After getting some advice from another forum, I changed the min SDK version from 10 or 11 to 17, since this is just a learning activity. Currently, I have the "Share" button showing up in the action bar, but tapping on it does nothing. I tried putting it in the overflow menu, but still, nothing. I tried some debugging, but I don't know where the button click is supposed to be handled; the debugger goes through and creates the shareIntent object, but then nothing seems to happen with it. I looked at this doc, but when I try to handle the sharing in the view's onOptionsItemSelected, I get a null pointer exception on the call to createShareIntent. What am I missing?

Here's the nested fragment's onCreateOptionsMenu:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.detail_fragment, menu);
    MenuItem item = menu.findItem(R.id.action_share);

    ShareActionProvider mShareActionProvider = new ShareActionProvider(getActivity());
    if(mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(createShareIntent());
    } else {
        Log.d(LOG_TAG, "Share action provider is null");
    }
}

Here's the containing view's onOptionsItemSelected, with the problematic code commented out:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        startActivity(new Intent(this, SettingsActivity.class));
        return true;
    } else if (id == R.id.action_share) {
        //DetailFragment details = (DetailFragment) getFragmentManager().findFragmentByTag("detailFragment");
        //startActivity(details.createShareIntent());
    }

    return super.onOptionsItemSelected(item);
}

And here's the createShareIntent method:

private Intent createShareIntent() {
    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, mForecastStr + FORECAST_SHARE_HASHTAG);
    return shareIntent;
}
spartanhooah
  • 183
  • 4
  • 15

2 Answers2

0

Currently, I have the "Share" button showing up in the action bar, but tapping on it does nothing.

Make sure you are going through your code branch that calls setShareIntent(), and make sure that the device or emulator you are testing on has an activity from some app that supports ACTION_SEND for text/plain. Also, if there is only one activity that supports ACTION_SEND for text/plain, the icon for that activity will appear adjacent to the ShareActionProvider share icon, and you would tap the activity icon to share with that activity.

I tried some debugging, but I don't know where the button click is supposed to be handled

That is inside the implementation of ShareActionProvider.

I looked at this doc, but when I try to handle the sharing in the view's onOptionsItemSelected, I get a null pointer exception on the call to createShareIntent.

onOptionsItemSelected() is for regular action items, not action providers.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Yes, when I debug, I get into the `if(mShareActionProvider != null)` block. I'm running the app on my own Nexus 4, which I believe has plenty of share activities. Your other comments are helpful for clarity. – spartanhooah Dec 29 '14 at 17:53
  • @spartanhooah: FWIW, here is a complete sample app that implements the same sort of `ShareActionProvider` that you are trying: https://github.com/commonsguy/cw-omnibus/tree/master/ActionBar/ShareNative – CommonsWare Dec 29 '14 at 17:57
  • I looked at that implementation, and tried doing something similar in the onCreateOptionsMenu method, but apparently I can't get a valid ActionProvider object from the MenuItem - I saw the "Share action provider is null" message in the log when I tried that route. – spartanhooah Dec 29 '14 at 20:51
  • @spartanhooah: Make sure that you have synchronized your action bar and `ShareActionProvider` implementations. The sample that I linked to uses the native action bar and native `ShareActionProvider`. If you are using `appcompat-v7`, or ActionBarSherlock, there will be other classes you need to use (and, in the case of `appcompat-v7`, different menu XML resource attributes). – CommonsWare Dec 29 '14 at 20:57
  • I was using the the `appcompat-v7` library, but switched to native `ShareActionProvider`. What do you mean by synchronizing the implementations? – spartanhooah Dec 29 '14 at 21:25
  • @spartanhooah: If you are using the `appcompat-v7` action bar (e.g., `ActionBarActivity`), you need to use the `appcompat-v7` version of `ShareActionProvider`, not the native one. – CommonsWare Dec 29 '14 at 21:38
  • I'm not using the compatibility library at all. (Also, sorry about moving this; I mis-tapped on my phone.) – spartanhooah Dec 30 '14 at 22:54
0

It turns out I had put android:actionProviderClass="android.widget.ShareActionProvider in a layout XML file instead of a menu one. I don't think the Udacity course I'm taking mentioned the correct location for that (perhaps due to compatibility things).

spartanhooah
  • 183
  • 4
  • 15