Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ResourceNotFoundException in preview custom View

I've got a custom view, let's call it CustomView. In the initialization, I call the following method

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

In the app, this view works perfectly and the font is displayed as it should but the problem is that the preview of the view wont load in the editor with the following details:

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)

Why won't this resource be loaded in the editor but it has no problem running in the app? FYI, MinSdk = 21 and TargetSdk = 28

like image 709
Wouter Vandenputte Avatar asked Aug 20 '19 20:08

Wouter Vandenputte


1 Answers

Don't call setTypeface in design mode. Use:

if (!isInEditMode()) {
textPaint.setTypeface(ResourcesCompat.getFont(getContext(),R.font.pretty_font))
}
like image 121
user1209216 Avatar answered Sep 28 '22 14:09

user1209216