user3290180
user3290180

Reputation: 4410

Android: menu option ShareAction versions

It is compiled without errors but I get this at runtime after the activity crashed:

 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

The line of code is in onCreateOptionsMenu()

shareProvider = (ShareActionProvider) item.getActionProvider();

It said to use MenuItemCompat.getActionProvider() but it's in another import and I have minSdkVersion 15, targetSdkVersion 21

this is the 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

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

Upvotes: 0

Views: 410

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 199825

When you use ActionBarActivity, you must use android.support.v7.widget.ShareActionProvider instead of android.widget.ShareActionProvider:

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

And then use MenuItemCompat.getActionProvider() as the error message implies.

Note that the ShareActionProvider only becomes clickable after setShareIntent() is called - you should do that as soon as you can (either in onCreateOptionsMenu() or whenever the content you want to share is loaded/selected/ready rather than in onOptionsItemSelected().

Upvotes: 2

Related Questions