gpt4 book ai didi

android - 如何使用支持操作栏将默认主页显示为向上指示器?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:24:54 25 4
gpt4 key购买 nike

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

我试过

getSupportActionBar().setHomeAsUpIndicator(0);

根据documentation说将使用主题默认值。当我这样做时,应用程序崩溃并出现以下错误:

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)

我知道我总是可以将默认主页作为向上指示器 Assets 复制到我的项目中并直接使用该 Assets ,但我想尽可能避免这种解决方法。

谢谢!

扎克

编辑:

我也试过

getSupportActionBar().setHomeAsUpIndicator(null);

这导致没有显示任何图标。

最佳答案

我遵循以下源代码:ToolbarActionBar.setHomeAsUpIndicator()

当我看到源代码时:ContextCompat.getDrawable() ,它说:“值 0 是无效标识符”。

 /**
* 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);
}
}

我一直关注源代码,最后看到下面的代码,这是你抛出异常的地方:Resources.getValue()

/**
* 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));
}

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

关于android - 如何使用支持操作栏将默认主页显示为向上指示器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30262659/

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