0

分享菜单项

<item
    android:id="@+id/action_share"
    android:title="@string/action_share"
    android:orderInCategory="2"
    app:showAsAction="ifRoom"
    android:actionProviderClass="android.widget.ShareActionProvider" />

MainActivity.java

@Override

public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main, menu);
    MenuItem menuItem = menu.findItem(R.id.action_share);
    shareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
    setIntent("This is a sample text");
    return true;
}

private void setIntent(String string)
{
    Intent intent = new Intent(android.content.Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(android.content.Intent.EXTRA_TEXT,string);
    shareActionProvider.setShareIntent(intent);
}

@Override

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

    if (id == R.id.action_share)
    {

    }

这根本不起作用,应用程序崩溃。

日志猫是 -

03-09 21:38:46.567 30763-30763/? E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.example.pooja1.bitsandpizzas, PID: 30763
   java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.getActionProvider()
       at android.support.v7.view.menu.MenuItemImpl.getActionProvider(MenuItemImpl.java:645)
       at com.example.pooja1.bitsandpizzas.MainActivity.onCreateOptionsMenu(MainActivity.java:31)
       at android.app.Activity.onCreatePanelMenu(Activity.java:2862)
       at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:341)

MainActivity.java:31 行 -> shareActionProvider = (ShareActionProvider) menuItem.getActionProvider();

提前致谢!

4

2 回答 2

0

Try change android:actionProviderClass="android.widget.ShareActionProvider" to app:actionProviderClass="android.support.v7.widget.ShareActionProvider"

于 2016-03-16T20:49:11.973 回答
0

好吧,它清楚地写在使用的异常中

MenuItemCompat.getActionProvider()

由 android v7 支持库提供,而不是使用

menuItem.getActionProvider();

默认android提供的方法。

于 2016-03-09T16:27:28.240 回答