gpt4 book ai didi

java.lang.NoSuchMethodException : setHomeActionContentDescription [int]? 异常

转载 作者:行者123 更新时间:2023-11-30 02:31:38 26 4
gpt4 key购买 nike

当我使用反射获取方法 setHomeActionContentDescription 时,如下所示:

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

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

最佳答案

遗憾的是,在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 或任何你想要的。

关于java.lang.NoSuchMethodException : setHomeActionContentDescription [int]? 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27246978/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com