ActionProvider 无法转换为 ShareActionProvider

ActionProvider cannot be converted to ShareActionProvider

import android.widget.ShareActionProvider;
...
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.detailfragment, menu);
    MenuItem menuItem = menu.findItem(R.id.action_share);
    ShareActionProvider mShareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(menuItem);

    if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(createShareForecastIntent());
    } else {
        Log.d(LOG_TAG, "Share Action Provider is null?");
    }
}

此代码显示以下错误:

incompatible types: ActionProvider cannot be converted to ShareActionProvider

以下是detailfragment.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>

请帮忙。

android.widget.ShareActionProviderandroid.support.v7.widget.ShareActionProvider不一样class。更改您的导入以匹配您的 actionProviderClass.

对我有用,试一试。 import android.support.v7.widget.ShareActionProvider 删除它并添加此导入: import android.widget.ShareActionProvider;