0

I have this code in a class that uses ShareActionProvider

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    try
    {
        getMenuInflater().inflate(R.layout.menu, menu);
        MenuItem item = menu.findItem(R.id.menu_item_share);
        myShareActionProvider = (ShareActionProvider)item.getActionProvider();
        myShareActionProvider.setShareHistoryFileName(
          ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
        myShareActionProvider.setShareIntent(createShareIntent());
        return true;
    }
    catch ( Exception e )
    {

    }

    return false;
}

This is the Android OS 2.3.6

And the crash said this

java.lang.NoSuchMethodError: android.view.MenuItem.getActionProvider
        at com.marketing.AdvertisingActivity.onCreateOptionsMenu(AdvertisingActivity.java:183)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2194)
        at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:360)
        at com.android.internal.policy.impl.PhoneWindow.onKeyDownPanel(PhoneWindow.java:605)
        at com.android.internal.policy.impl.PhoneWindow.onKeyDown(PhoneWindow.java:1259)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1717)
        at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2607)
        at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2582)
        at android.view.ViewRoot.handleMessage(ViewRoot.java:1914)
        at android.os.Handler.dispatchMessage(Handler.java:130)
        at android.os.Looper.loop(SourceFile:351)
        at android.app.ActivityThread.main(ActivityThread.java:3814)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:538)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:659)
        at dalvik.system.NativeStart.main(Native Method)

How can that crash the app when it is inside the try/catch block?

Thanks!

Genadinik
  • 18,153
  • 63
  • 185
  • 284
  • Did you add the activity in Advertising.java to your Android.manifest? – ACengiz May 07 '13 at 22:28
  • This is bizarre. Are you absolutely sure that the error is being generated in that specific try block? For the sake of my sanity (and yours), drop some Log.i() statements before and after myShareActionProvider = (ShareActionProvider)item.getActionProvider(); It would also be helpful to see your ShareActionProvider class. – MarsAtomic May 07 '13 at 22:38

2 Answers2

2

The trace points to getActionProvider(); there's no method available on MenuItem with that name. If you look at the documentation for MenuItem, you'll see that getActionProvider() is only available on devices running at level 14 (i.e. 4.0.x) or over. Presumably (there's a line in your code with "This is the Android OS 2.3.6") you're not running this code on a device that meets that requirement.

You're trying to catch an Exception; NoSuchMethodError is an Error. It's not recommended that you catch these. You should just check the version number.

James
  • 3,729
  • 3
  • 20
  • 16
0

you should know more about android v4, v7 and v7 appcompat.

try using

MenuItemCompat.getActionProvider(item);

in your share_menu.xml verify you are using "yourApp:actionProviderClass" instead of "android:action" and give the value as

"android.support.v7.widget.ShareActionProvider"

Add xmlns:yourApp="http://schemas.android.com/apk/res-auto" to xml namespace.

yourApp is a String of your choice. Hope this helps.

LeoCoder
  • 11
  • 2