简体   繁体   中英

Eclipse error: cannot be resolved to a type

With a friend's help, I made a simple app for school that loads a webpage like an iframe into it. Next, I need to learn how to add a share button so it's users can tell their friends about my app. I found this code and tried to add it to my files in eclipse, but it gives me some errors. I have searched a lot for the solution and I think there are several possibilities to solve it, such as refreshing the project, however I'm so new I'm just worried that I'll mess up my project somehow, so I feel it's best to ask first :)

This is the code I added:

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.action_bar_share_menu, menu);
    MenuItem item = menu.findItem(R.id.menu_item_share);

    ShareActionProvider myShareActionProvider = (ShareActionProvider) item.getActionProvider();

    Intent myIntent = new Intent();
    myIntent.setAction(Intent.ACTION_SEND);
    myIntent.putExtra(Intent.EXTRA_TEXT, "Whatever message you want to share");
    myIntent.setType("text/plain");

    myShareActionProvider.setShareIntent(myIntent);

    return true;
}

And the errors are:

  1. Menu cannot be resolved to a type
  2. MenuItem cannot be resolved to a type
  3. ShareActionProvider cannot be resolved to a type
  4. ShareActionProvider cannot be resolved to a type (yes the same error twice)

Hope someone can help. Thanks all.

are your import statements proper? try:

  1. project Clean and Rebuild
  2. android sdk selected correctly

Have you imported them?

import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;

Also note that ShareActionProvider is only available in Ice Cream (API 14) or newer. If you are targeting a lower API level you have to use

import android.support.v7.widget.ShareActionProvider;

and add the support library to your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM