gpt4 book ai didi

预览自定义 View 中的 Android ResourceNotFoundException

转载 作者:行者123 更新时间:2023-12-02 11:18:27 26 4
gpt4 key购买 nike

我有一个自定义 View ,我们将其称为CustomView。在初始化时,我调用了以下方法

textPaint.setTypeface(ResourcesCompat.getFont(getContext(),R.font.pretty_font));

在应用程序中,此 View 工作完美,并且字体按应有的方式显示,但问题是 View 的预览不会在编辑器中加载,并包含以下详细信息:

android.content.res.Resources$NotFoundException: Font resource ID

0x7f090000 could not be retrieved.

at androidx.core.content.res.ResourcesCompat.loadFont(ResourcesCompat.java:365) at androidx.core.content.res.ResourcesCompat.getFont(ResourcesCompat.java:230) at com.mycompany.myapp.view.views.CustomView.init(CustomView.java:490) at com.mycompany.myapp.view.views.CustomView.initAttrs(CustomView.java:484) at com.mycompany.myapp.view.views.CustomView.(CustomView.java:116) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:403) at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:186) at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:144) at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:309) at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:418) at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:429) at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:333) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:863) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72) at android.view.LayoutInflater.rInflate(LayoutInflater.java:837) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:866) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72) at android.view.LayoutInflater.rInflate(LayoutInflater.java:837) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:394) at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:323) at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:394) at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:200) at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:572) at com.android.tools.idea.rendering.RenderTask.lambda$inflate$5(RenderTask.java:698) at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)

为什么这个资源在编辑器中加载不出来,但在应用程序中运行却没有问题?仅供引用,MinSdk = 21 且 TargetSdk = 28

最佳答案

不要在设计模式下调用setTypeface。使用:

if (!isInEditMode()) {
textPaint.setTypeface(ResourcesCompat.getFont(getContext(),R.font.pretty_font))
}

关于预览自定义 View 中的 Android ResourceNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57580824/

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