Android:不支持此功能,请使用MenuItemCompat.getActionProvider()

时间:2015-10-30 11:16:47

标签: android share menuitem

我的Activity中有一个TextView,我在SQLite的TextView中加载数据

在该活动中,我有一个菜单选项"分享"。
当我点击该图标时,我的活动崩溃了。

这是代码和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.access$100(AppCompatDelegateImplV7.java:90)
            at android.support.v7.app.AppCompatDelegateImplV7$1.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)

3 个答案:

答案 0 :(得分:4)

首先,将android.widget.ShareActionProvider与appcompat-v7操作栏反向端口(例如,ActionBarActivity)一起使用。使用appcompat-v7版本的ShareActionProvider,或将所有内容移动到本机操作栏。

  

因此,更改公共类活动扩展 AppCompatActivity 并导入类 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);

    }

答案 1 :(得分:0)

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);

答案 2 :(得分:0)

我想你错过了

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..."));