Wednesday 15 September 2010

Android: menu option ShareAction versions -


It is compiled without any errors, but after crashing the activity, I get it at runtime:

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

line of code (the onCreateOptionsMenu)

  shareProvider = (ShareActionProvider) item.getActionProvider ();  

MenuItemCompat.getActionProvider (use) asked for, but it is an import and I minSdkVersion is 15, targetSdkVersion 21

This activity

>  import android.app.AlertDialog; Android Import Bluetooth. Bluetooth adapter; 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; Importroid.os.Bundle; Import android Preference 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 ActionBarActivity implements AdapterView.OnItemLongClickListener {Private ShareActionProvider shareProvider; @ Override public boolean on cret option menu (menu menu) {getMenuInflater (). Flood (R. menu.menu, menu); Menu item item = menu.findime (raid.action_share); ShareProvider = (ShareActionProvider) item.getActionProvider (); Back true; } @Override public boolean onOptionsItemSelected (MenuItem item) {Intent shareIntent = new Intent (Intent.ACTION_SEND) .putExtra (Intent.EXTRA_TEXT, "text"); SetShareIntent (shareIntent); Back true; } Private Zero SetSheseentant (Intent Shareentent) {if (SharePrice! = Faucet) Share Provider .SetShareEmantant (Share Intent); }}  

Menu

  & lt; 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 "tool: reference =" com ....... activity "& gt; & Lt; item android: id = "@ + id / action_share" android: title = "Share" app: Sosakshn = "Ifroom" android: icon = "@ android: drawable / Aisi_menu_shaire" Android: ActionProviderClass = "AndroidkwidgetkShareActionProvider" / & Gt; & Lt; / Menu & gt;    

When you use ActionBarActivity Instead of using android.widget.ShareActionProvider :

  & lt; Android: id = "@ + id / action_share" Android: title = "share" application: showAsAction = "ifRoom" app: action providerClass = "android.support.v7.widget.ShareActionProvider" /> Errors the error message as  

and then MenuItemCompat.getActionProvider () .

Use the note that it is clickable only after calling ShareActionProvider setShareIntent () - as soon as you can ( either onCreateOptionsMenu () is used or when you want to share, load content / onOptionsItemSelected () .

instead selected /

No comments:

Post a Comment