(4.3.1.16)java.lang.NoSuchMethodException: setHomeActionContentDescription

来源:互联网 发布:淘宝买家敲诈食品卖家 编辑:程序博客网 时间:2024/06/05 17:37

当使用反射来获取方法 setHomeActionContentDescription 像这样:

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

我得到 NoSuchMethodException:java.lang.NoSuchMethodException: setHomeActionContentDescription [int]

解决方法 1:

不幸的是,此方法不可用在 API 18 岁之前。即使使用反射,方法不是可用的。如果您想使用此方法,您可以从 SherlockNavigationDrawer 库使用一种变通方法:

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

资料来源:https://github.com/nicolasjafelle/SherlockNavigationDrawer/blob/master/SherlockNavigationDrawer/src/com/sherlock/navigationdrawer/compat/SherlockActionBarDrawerToggleHoneycomb.java#L97

如你所看到的如果它是不可能来访问方法,他尝试直接得到 ImageView 。当你拥有它时,你可以直接做的 setContentDescription 或任何你想要的东西。

0 0