Unable to instantiate class: app.support.v7.widget.ShareActionProvider

I am trying to use ShareActionProvider in DetailFragment class.

public static class DetailFragment extends Fragment {

    private static final String LOG_TAG = DetailFragment.class.getSimpleName();
    private static final String FORECAST_SHARE_HASHTAG = " # SunshineApp";
    private String mForecastStr;

    public DetailFragment() {
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

       View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
       Intent intent = getActivity().getIntent();

       if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
            mForecastStr = intent.getStringExtra(Intent.EXTRA_TEXT);
            TextView info = (TextView)rootView.findViewById(R.id.info);
            Log.d(LOG_TAG, "mForecastStr is :  " + mForecastStr);
            info.setText(mForecastStr);
        }

       return rootView;
    }

    @Override
    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?");
        }
    }

    private Intent createShareForecastIntent() {
        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;
    }
}

--EOF--

      

But I can not only get a null class instead of mShareActionProvider. The error messages are here.

07-19 18:07:36.348: W/SupportMenuInflater(31261): Cannot instantiate class: app.support.v7.widget.ShareActionProvider
07-19 18:07:36.348: W/SupportMenuInflater(31261): java.lang.ClassNotFoundException: Didn't find class "app.support.v7.widget.ShareActionProvider" on path: DexPathList[[zip file "/data/app/com.example.project-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.project-1, /vendor/lib, /system/lib]]

      

It seems that Android Studio cannot find app.support.v7.widget.ShareActionProvider. I have already installed Android Support Library. What should I do?

Update !!

This is my build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.shinjaehun.sunshine"
        minSdkVersion 10
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        } 
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:support-v4:22.2.1'
    compile 'com.android.support:appcompat-v7:22.2.1'
}

--EOF---

      

I am using SDK build Tools version 22.0.1 but my app library version is 22.2.1. This is the reason why I fail, right? How do I use appcompat version 22.0.1? There is only Android support library version 22.2.1.

+3


source to share





All Articles