Android:菜单选项ShareAction版本

时间:2015-03-17 18:03:35

标签: android android-intent share android-optionsmenu

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

 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

这是活动

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>

1 个答案:

答案 0 :(得分:2)

使用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()或在您要共享的内容加载时执行此操作/ selected / ready而不是onOptionsItemSelected()