Android :不支持,使用MenuItemCompat.getActionProvider()

Android :This is not supported, use MenuItemCompat.getActionProvider()

我的 Activity 中有一个 TextView,我从 SQLite

中将数据加载到该 TextView 中

其中 Activity 我有一个菜单选项 "Share"。
当我单击该图标时,我的 Activity 崩溃了。

这是代码,LogCat错误

 ShareActionProvider provider;
public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        detailtext= (TextView) findViewById(R.id.detail);
       if (id==R.id.menu_item_share)
        {
                doShare();
        }
 return super.onOptionsItemSelected(item);
}

 public void doShare() {
        // populate the share intent with data
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, "This is a message for you");
        provider.setShareIntent(intent);
    }

Menu.xml

<item android:id="@+id/menu_item_share"
    android:showAsAction="ifRoom"
    android:title="Share"
    android:icon="@mipmap/menu_item_share"
    android:actionProviderClass="android.widget.ShareActionProvider" />

Logcat:

java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.getActionProvider()
            at android.support.v7.internal.view.menu.MenuItemImpl.getActionProvider(MenuItemImpl.java:645)
            at com.example.aeiltech.sidd.DetailActivity.onCreateOptionsMenu(DetailActivity.java:115)
            at android.app.Activity.onCreatePanelMenu(Activity.java:2661)
            at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:262)
            at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
            at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:267)
            at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1221)
            at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1501)
            at android.support.v7.app.AppCompatDelegateImplV7.access0(AppCompatDelegateImplV7.java:90)
            at android.support.v7.app.AppCompatDelegateImplV7.run(AppCompatDelegateImplV7.java:128)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5641)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1288)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
            at dalvik.system.NativeStart.main(Native Method)
public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. 
int id = item.getItemId(); 
detailtext= (TextView) findViewById(R.id.detail); 
if (id==R.id.menu_item_share) 
{ 
  doShare(); 
  provider=item.getActionProvider();       
}return super.onOptionsItemSelected(item);

我想你错过了

startActivity(Intent.createChooser(emailIntent, "Tell a friend..."));  

看这个例子

 Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("text/plain");
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Awesome Application...");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                "Hi, I found this application  on Google Play Store. ");
    emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

                startActivity(Intent.createChooser(emailIntent, "Tell a friend..."));

首先,将 android.widget.ShareActionProvider 与 appcompat-v7 操作栏向后移植(例如 ActionBarActivity)结合使用。要么使用 ShareActionProvider 的 appcompat-v7 版本,要么将所有内容移至本机操作栏。

So Change public class Activity extends AppCompatActivity and import the class import android.support.v7.widget.ShareActionProvider;

我想分享 textview 的详细信息,所以使用

SelectedText=detailtext.getText().toString();

public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

       getMenuInflater().inflate(R.menu.menu_detail, menu);
       MenuItem shareItem = menu.findItem(R.id.menu_item_share);

        mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);

        return true;
}

 public boolean onOptionsItemSelected(MenuItem item) {
 int id = item.getItemId();

if (id==R.id.menu_item_share)
        {
                doShare();


        }



        return super.onOptionsItemSelected(item);
    }
    public void doShare() {
        // populate the share intent with data

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT,SelectedText);
        mShareActionProvider.setShareIntent(intent);

    }