3

I'm 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't only get null class instead of mShareActionProvider. 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 Can't find app.support.v7.widget.ShareActionProvider. I 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'm using SDK build Tools Version 22.0.1 but my appcompat library version is 22.2.1. It's the reason why I fail, isn't it? How can I use appcompat version 22.0.1? There is an Android Support Library Ver 22.2.1 only.

gunsmoke
  • 51
  • 5
  • It's possible that your app module doesn't have a dependency on the support library. Can you paste your module's `build.gradle` file? – Aron Lorincz Jul 19 '15 at 12:38
  • Thanks, Wand maker! But could you tell me more? I recently moved from Eclipse to Android studio. – gunsmoke Jul 20 '15 at 00:35
  • Sorry, I think my tip wasn't correct. If it's a `ClassNotFoundError`, try `Build`->`Clean`, then run it again. – Aron Lorincz Jul 20 '15 at 03:30
  • Thanks, Aron! I'll try. – gunsmoke Jul 20 '15 at 08:34
  • I solved this problem. I defined actionProviderClass in memu item as 'android.support.v7.widget.ShareActionProvider' but indeed I typed as 'app.support.v7.widget.ShareActionProvider' At last I realized my mistake. I am feelling ashamed of myself. – gunsmoke Jul 25 '15 at 09:21

0 Answers0