Genadinik
Genadinik

Reputation: 18639

Android - ShareActionProvider exception inside a try/catch crashed the app

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!

Upvotes: 0

Views: 357

Answers (2)

LeoCoder
LeoCoder

Reputation: 11

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.

Upvotes: 0

James
James

Reputation: 3749

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.

Upvotes: 2

Related Questions