Android: 菜单选项 ShareAction 版本

Android: menu option ShareAction versions

它编译没有错误,但我在 activity 崩溃后在运行时得到这个:

 java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.getActionProvider()
        at android.support.v7.internal.view.menu.MenuItemImpl.getActionProvider(MenuItemImpl.java:645)
        at com.......Activity.onCreateOptionsMenu

这行代码在onCreateOptionsMenu()

shareProvider = (ShareActionProvider) item.getActionProvider();

它说要使用 MenuItemCompat.getActionProvider() 但它在另一个导入中,我有 minSdkVersion 15,targetSdkVersion 21

这是activity

import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ShareActionProvider;
import android.widget.TextView;
import android.widget.Toast;

public class Activity extends ActionBarActivity implements AdapterView.OnItemLongClickListener 
 {
    private ShareActionProvider shareProvider;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        MenuItem item = menu.findItem(R.id.action_share);
        shareProvider = (ShareActionProvider) item.getActionProvider();
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent shareIntent = new Intent(Intent.ACTION_SEND)
                          .putExtra(Intent.EXTRA_TEXT, "text");
        setShareIntent(shareIntent);
        return true;
    }

    private void setShareIntent(Intent shareIntent) {
        if (shareProvider != null) 
             shareProvider.setShareIntent(shareIntent);
    }
}

菜单

<menu xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   tools:context="com.......Activity">
    <item android:id="@+id/action_share"
    android:title="Share" app:showAsAction="ifRoom"
    android:icon="@android:drawable/ic_menu_share"
    android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>

当你使用ActionBarActivity时,你必须使用android.support.v7.widget.ShareActionProvider而不是android.widget.ShareActionProvider:

<item android:id="@+id/action_share"
    android:title="Share" app:showAsAction="ifRoom"
    app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />

然后按照错误信息的提示使用MenuItemCompat.getActionProvider()

请注意,ShareActionProvider 仅在调用 setShareIntent() 后才可单击 - 您应该尽快执行此操作(在 onCreateOptionsMenu() 中或您想要共享的内容时是 loaded/selected/ready 而不是 onOptionsItemSelected().