Android 如何使用支持操作栏将默认主屏幕显示为向上指示器?

Android 如何使用支持操作栏将默认主屏幕显示为向上指示器?,android,android-actionbar,android-support-library,android-toolbar,Android,Android Actionbar,Android Support Library,Android Toolbar,在我的应用程序中,在某些情况下,我将home设置为非默认可绘制的向上指示器。有时,home as up(主页向上)指示器在更改后应重置为默认图标 我试过使用 getSupportActionBar().setHomeAsUpIndicator(0); 根据Say的说法,它将使用默认主题。执行此操作时,应用程序会崩溃,并出现以下错误: 05-15 11:12:03.160: E/AndroidRuntime(13021): FATAL EXCEPTION: main 05-15 11:12:03

在我的应用程序中,在某些情况下,我将home设置为非默认可绘制的向上指示器。有时,home as up(主页向上)指示器在更改后应重置为默认图标

我试过使用

getSupportActionBar().setHomeAsUpIndicator(0);
根据Say的说法,它将使用默认主题。执行此操作时,应用程序会崩溃,并出现以下错误:

05-15 11:12:03.160: E/AndroidRuntime(13021): FATAL EXCEPTION: main
05-15 11:12:03.160: E/AndroidRuntime(13021): Process: xxxxx.yyyyyy.zzzzzz, PID: 13021
05-15 11:12:03.160: E/AndroidRuntime(13021): android.content.res.Resources$NotFoundException: Resource ID #0x0
05-15 11:12:03.160: E/AndroidRuntime(13021):    at android.content.res.Resources.getValue(Resources.java:1233)
05-15 11:12:03.160: E/AndroidRuntime(13021):    at android.content.res.Resources.getDrawable(Resources.java:756)
05-15 11:12:03.160: E/AndroidRuntime(13021):    at android.content.Context.getDrawable(Context.java:402)
05-15 11:12:03.160: E/AndroidRuntime(13021):    at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
05-15 11:12:03.160: E/AndroidRuntime(13021):    at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:319)
05-15 11:12:03.160: E/AndroidRuntime(13021):    at android.support.v7.internal.widget.TintManager.getDrawable(TintManager.java:133)
05-15 11:12:03.160: E/AndroidRuntime(13021):    at android.support.v7.widget.Toolbar.setNavigationIcon(Toolbar.java:754)
05-15 11:12:03.160: E/AndroidRuntime(13021):    at android.support.v7.internal.app.ToolbarActionBar.setHomeAsUpIndicator(ToolbarActionBar.java:176)
05-15 11:12:03.160: E/AndroidRuntime(13021):    at xxxxx.yyyyyy.zzzzzz.updateHomeIndicator(aaaaa.java:1711)
我知道主题默认值设置正确,因为如果我从未将自定义主页设置为向上指示器,则返回箭头显示良好。在尝试调用
setHomeAsUpIndicator(0)
之前,我正在调用
setDisplayHomeAsUpEnabled(true)

我知道我总是可以将默认的home作为up indicator资产复制到我的项目中,并直接使用该资产,但如果可能的话,我希望避免这种变通方法

谢谢

扎克

编辑:

我也试过了

getSupportActionBar().setHomeAsUpIndicator(null);

这将导致不显示任何图标。

我遵循以下源代码:

当我看到的源代码:,它说:“值0是一个无效的标识符”


到目前为止,我看不到对“0”的任何特殊处理。所以,我认为这是你在上面发布的官方文件中的一个缺陷。解决此崩溃错误的简单方法是使用现有标识符,就像默认图标id一样。

为什么要传递0而不是布尔值?setHomeAsUpIndicator()API需要可绘制的或int资源id值。据我所知,没有布尔参数API可以实现这一点。文档说明:
如果将0传递给此方法,将使用主题中的默认可绘制图标。
根据文档说明,这是完全有效的,但是,如果设置自定义图标,您似乎无法在之后使用零调用它以将其重新设置为默认图标。这一定是API的一个特例。另外,请注意,
setHomeButtonEnabled()
接受一个布尔值,这看起来不是问题所在。问题在于
setHomeAsUpIndicator()
。我明白了。谢谢你,丹尼尔。这似乎是我当时看到的情况,是的。
 /**
 * Return a drawable object associated with a particular resource ID.
 * <p>
 * Starting in {@link android.os.Build.VERSION_CODES#LOLLIPOP}, the returned
 * drawable will be styled for the specified Context's theme.
 *
 * @param id The desired resource identifier, as generated by the aapt tool.
 *            This integer encodes the package, type, and resource entry.
 *            The value 0 is an invalid identifier.
 * @return Drawable An object that can be used to draw this resource.
 */
public static final Drawable getDrawable(Context context, int id) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 21) {
        return ContextCompatApi21.getDrawable(context, id);
    } else {
        return context.getResources().getDrawable(id);
    }
}
/**
 * Return the raw data associated with a particular resource ID.
 * 
 * @param id The desired resource identifier, as generated by the aapt
 *           tool. This integer encodes the package, type, and resource
 *           entry. The value 0 is an invalid identifier.
 * @param outValue Object in which to place the resource data.
 * @param resolveRefs If true, a resource that is a reference to another
 *                    resource will be followed so that you receive the
 *                    actual final resource data.  If false, the TypedValue
 *                    will be filled in with the reference itself.
 *
 * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
 *
 */
public void getValue(int id, TypedValue outValue, boolean resolveRefs)
        throws NotFoundException {
    boolean found = mAssets.getResourceValue(id, 0, outValue, resolveRefs);
    if (found) {
        return;
    }
    throw new NotFoundException("Resource ID #0x"
                                + Integer.toHexString(id));
}