java.lang.NoSuchMethodException: setHomeActionContentDescription [int]?

1.9k Views Asked by At

When I use reflection to get method setHomeActionContentDescription like this:

Method setHomeActionContentDescription = ActionBar.class.getDeclaredMethod(
    "setHomeActionContentDescription", Integer.TYPE);

I get NoSuchMethodException: java.lang.NoSuchMethodException: setHomeActionContentDescription [int]

1

There are 1 best solutions below

0
On BEST ANSWER

Unfortunately, this method is not available before API 18. Even with the use of reflection, the method is not available. If you want use this method, you can use a workaround from SherlockNavigationDrawer library :

            try {
                setHomeAsUpIndicator = ActionBar.class.getDeclaredMethod("setHomeAsUpIndicator",
                        Drawable.class);
                setHomeActionContentDescription = ActionBar.class.getDeclaredMethod(
                        "setHomeActionContentDescription", Integer.TYPE);

                // If we got the method we won't need the stuff below.
                return;
            } catch (NoSuchMethodException e) {
                // Oh well. We'll use the other mechanism below instead.
            }

            final View home = activity.findViewById(android.R.id.home);
            if (home == null) {
                // Action bar doesn't have a known configuration, an OEM messed with things.
                return;
            }

            final ViewGroup parent = (ViewGroup) home.getParent();
            final int childCount = parent.getChildCount();
            if (childCount != 2) {
                // No idea which one will be the right one, an OEM messed with things.
                return;
            }

            final View first = parent.getChildAt(0);
            final View second = parent.getChildAt(1);
            final View up = first.getId() == android.R.id.home ? second : first;

            if (up instanceof ImageView) {
                // Jackpot! (Probably...)
                upIndicatorView = (ImageView) up;
            }

Source: https://github.com/nicolasjafelle/SherlockNavigationDrawer/blob/master/SherlockNavigationDrawer/src/com/sherlock/navigationdrawer/compat/SherlockActionBarDrawerToggleHoneycomb.java#L97

As you can see, if it is not possible to access methods, he try to directly get the ImageView. When you have it, you can directly do a setContentDescription or whatever you want.