2

Environment:

  1. Android API 25
  2. Android Studio 2.3.3

I'm trying to add share item to menu and this is my code, I've tried these solution and searching for three days but it doesn't solve my problem

  1. Unable to cast Action Provider to Share Action Provider
  2. why MenuItemCompat.getActionProvider returns null?

Menu xml resource detail_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_share"
        android:title="@string/action_share"
        app:showAsAction="always"
     app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
</menu>

The Fragment code:

package com.example.poula.sunshine;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.ShareActionProvider;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

/**
 * A placeholder fragment containing a simple view.
 */
public class DetailActivityFragment extends Fragment {

    private static final String LOG_TAG = DetailActivityFragment.class.getSimpleName();
    private static final String FORECAST_SHARE_STRING = "#SunshineApp";
    private ShareActionProvider mShareActionProvider;

    private String forecastStr;

    public DetailActivityFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_detail,menu);
        inflater.inflate(R.menu.detail_fragment,menu);

        MenuItem menuItem = menu.findItem(R.id.action_share);

        mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);

        if(mShareActionProvider != null){
            mShareActionProvider.setShareIntent(createShareForecastIntent());
        }else{
            Log.d(LOG_TAG, "Share Action Provider is null!!");
            Toast.makeText(getActivity(), "Share Action Provider is null!!", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Intent intent = getActivity().getIntent();
        View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
        if( !(intent == null) && intent.hasExtra(Intent.EXTRA_TEXT) ){
            forecastStr = intent.getStringExtra(Intent.EXTRA_TEXT);
            TextView tv = (TextView) rootView.findViewById(R.id.detail_text);
            tv.setText(forecastStr);
        }

        return rootView;
    }

    private Intent createShareForecastIntent(){
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, forecastStr+" "+FORECAST_SHARE_STRING);
        return shareIntent;
    }
}

Issues:

  1. Share item on the bar is not clickable

any ideas on how to solve these two issues ?

Poula Adel
  • 609
  • 1
  • 10
  • 33
  • Now after some tries .. I found that I'm not inflating my menu xml resource, and the action bar icon showed up .. but still having problem that the item share is not clickable.. why is that ? – Poula Adel Jun 27 '17 at 16:10
  • [https://stackoverflow.com/questions/12087164/shareactionprovider-not-clickable-and-not-rendering-properly-on-first-render](https://stackoverflow.com/questions/12087164/shareactionprovider-not-clickable-and-not-rendering-properly-on-first-render) was helpful for me – Bö macht Blau Oct 14 '17 at 17:46

0 Answers0