无法单击ShareAction Provider

时间:2016-06-02 05:19:32

标签: android shareactionprovider

我正在尝试点击手机上的ShareActionProvider按钮,但它完全没有响应。没有任何共享应用程序,如短信或电子邮件弹出。我在我的xml文件中将ShareActionProvider标记为menu_share,并使用onOptionsItemSelected方法来响应任何点击。

爪哇

import android.support.v7.widget.ShareActionProvider;

public class MainActivityFragment extends Fragment{
        ArrayAdapter<String> mForecastAdapter;
        //String[] parsedWeatherData;
        ShareActionProvider provider;


public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
       inflater.inflate(R.menu.forecastfragment, menu);
       MenuItem item = menu.findItem(R.id.menu_share);
       provider = (ShareActionProvider)      
       MenuItemCompat.getActionProvider(item);
    }

public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_share:
                Log.d("Weather", "Is menu share clicked");
                doShare();
                break;
            default:
                break;
        }
        return true;
    }

public void doShare() {
        String message = oneDayWeather;

        // populate the share intent with data


        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, message);
        provider.setShareIntent(intent);
    }

XML

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:bwq="http://schemas.android.com/apk/res-auto"
    >

    <item
        android:id="@+id/menu_share"
        android:title="@string/menu_share"
        bwq:actionProviderClass="android.support.v7.widget.ShareActionProvider"
        bwq:showAsAction="always"/>


</menu>

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

没关系,我解决了。只有在获取提供程序后立即设置setShareIntent方法时,此版本的ShareActionProvider才有效。例如:

        MenuItem item = menu.findItem(R.id.menu_share);
        provider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
        provider.setShareIntent(getDefaultShareIntent());
相关问题