具有自定义颜色属性的 TextView 不会在布局预览中呈现
TextView with custom color attribute not renders in Layout preview
TextView
自定义颜色属性未在布局预览中呈现。
我在值文件夹中定义了属性 attr.xml
。
<attr name="secondary_text_color" format="color"/>
并在 style
xml 中为深色和浅色主题覆盖此颜色。
里面 TextView
:
android:textColor="?attr/secondary_text_color"
布局预览显示错误,但应用程序按预期运行。
java.lang.NullPointerException
at android.widget.TextView.updateTextColors(TextView.java:5773)
at android.widget.TextView.setHintTextColor(TextView.java:5047)
at android.widget.TextView.applyTextAppearance(TextView.java:4053)
at android.widget.TextView.(TextView.java:1604)
at android.widget.TextView.(TextView.java:968)
at android.widget.TextView.(TextView.java:964)
at sun.reflect.GeneratedConstructorAccessor273.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:854)
at android.view.LayoutInflater.createView(LayoutInflater.java:776)
at android.view.BridgeInflater.onCreateView(BridgeInflater.java:129)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:930)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:950)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:308)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1123)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1097)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:501)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:353)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:404)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:713)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate(RenderTask.java:844)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1604)
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)
您应该在布局预览控制面板中将默认主题更改为所需的主题,在本例中为浅色或深色主题。
TextView
自定义颜色属性未在布局预览中呈现。
我在值文件夹中定义了属性 attr.xml
。
<attr name="secondary_text_color" format="color"/>
并在 style
xml 中为深色和浅色主题覆盖此颜色。
里面 TextView
:
android:textColor="?attr/secondary_text_color"
布局预览显示错误,但应用程序按预期运行。
java.lang.NullPointerException at android.widget.TextView.updateTextColors(TextView.java:5773) at android.widget.TextView.setHintTextColor(TextView.java:5047) at android.widget.TextView.applyTextAppearance(TextView.java:4053) at android.widget.TextView.(TextView.java:1604) at android.widget.TextView.(TextView.java:968) at android.widget.TextView.(TextView.java:964) at sun.reflect.GeneratedConstructorAccessor273.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at android.view.LayoutInflater.createView(LayoutInflater.java:854) at android.view.LayoutInflater.createView(LayoutInflater.java:776) at android.view.BridgeInflater.onCreateView(BridgeInflater.java:129) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:930) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:950) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004) at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:308) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1123) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72) at android.view.LayoutInflater.rInflate(LayoutInflater.java:1097) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084) at android.view.LayoutInflater.inflate(LayoutInflater.java:682) at android.view.LayoutInflater.inflate(LayoutInflater.java:501) at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:353) at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:404) at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141) at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:713) at com.android.tools.idea.rendering.RenderTask.lambda$inflate(RenderTask.java:844) at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1604) 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)
您应该在布局预览控制面板中将默认主题更改为所需的主题,在本例中为浅色或深色主题。