android 上的 SwitchCompat 渲染问题
SwitchCompat rendering issues on android
我在我的一个布局中使用 android.support.v7.widget.SwitchCompat。
我的编译sdk版本是25,buildTools版本是25.0.1,com.android.support:design我用的是25.2.0版本
我已经重新启动 android 工作室,使缓存无效,clean/build。但是,我仍然无法从我的设计视图中摆脱这个错误。知道我错过了什么吗?
这是我的风格:
<style name="AppTheme" parent="android:Theme.Holo.Light">
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
<item name="android:textViewStyle">@style/RobotoTextViewStyle</item>
<item name="android:editTextStyle">@style/RobotoEditTextStyle</item>
</style>
<style name="SplashTheme" parent="android:Theme.Holo.Light">
<item name="android:windowBackground">@drawable/splash</item>
</style>
<style name="RobotoTextViewStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">sans-serif</item>
<item name="android:textSize">@dimen/body</item>
<item name="android:textColor">@color/colorBlack</item>
</style>
<style name="RobotoEditTextStyle" parent="android:Widget.EditText">
<item name="android:fontFamily">sans-serif</item>
<item name="android:textSize">@dimen/body</item>
<item name="android:padding">@dimen/edittext_padding</item>
</style>
<style name="CustomTabLayout" parent="CustomTabLayout">
<item name="tabIndicatorColor">@color/colorGold</item>
<item name="tabSelectedTextColor">@color/colorGold</item>
</style>
<style name="StyledScrollerTextAppearance">
<item name="android:textColor">@color/colorWhite</item>
</style>
<style name="CustomSwitch">@style/CustomSwitch</style>
错误如下
java.lang.NullPointerException
at android.text.StaticLayout.<init>(StaticLayout.java:422)
at android.support.v7.widget.SwitchCompat.makeLayout(SwitchCompat.java:864)
at android.support.v7.widget.SwitchCompat.onMeasure_Original(SwitchCompat.java:783)
at android.support.v7.widget.SwitchCompat.onMeasure(SwitchCompat.java)
at android.view.View.measure(View.java:19731)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:927)
at android.support.constraint.ConstraintLayout.onMeasure_Original(ConstraintLayout.java:966)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java)
at android.view.View.measure(View.java:19731)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:927)
at android.support.constraint.ConstraintLayout.onMeasure_Original(ConstraintLayout.java:966)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java)
at android.view.View.measure(View.java:19731)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1293)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.widget.ScrollView.onMeasure(ScrollView.java:340)
at android.view.View.measure(View.java:19731)
at android.support.constraint.ConstraintLayout.onMeasure_Original(ConstraintLayout.java:1021)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java)
at android.view.View.measure(View.java:19731)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19731)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19731)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.measureView(RenderSessionImpl.java:545)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:342)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:389)
at com.android.tools.idea.rendering.RenderTask.compute(RenderTask.java:548)
at com.android.tools.idea.rendering.RenderTask.compute(RenderTask.java:533)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:966)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:533)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate(RenderTask.java:659)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
这是我的部分布局xml
<android.support.constraint.ConstraintLayout
android:id="@+id/saveCopyContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/saveCopyBorderTop">
<TextView
android:id="@+id/saveCopyLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Save a copy"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<android.support.v7.widget.SwitchCompat
android:id="@+id/saveCopySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:checked="true"
android:theme="@style/CustomSwitch"
app:layout_constraintBottom_toBottomOf="@+id/saveCopyLabel"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="@+id/saveCopyLabel"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/saveCopyLabel"/>
</android.support.constraint.ConstraintLayout>
SwitchCompat
需要这两个:
android:textOn
和
android:textOff
所以在你xml变化:
<android.support.v7.widget.SwitchCompat
android:id="@+id/saveCopySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:textOn="On" //add this
android:textOff="Off" //add this
android:checked="true"
android:theme="@style/CustomSwitch"
app:layout_constraintBottom_toBottomOf="@+id/saveCopyLabel"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="@+id/saveCopyLabel"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/saveCopyLabel"/>
我在我的一个布局中使用 android.support.v7.widget.SwitchCompat。 我的编译sdk版本是25,buildTools版本是25.0.1,com.android.support:design我用的是25.2.0版本 我已经重新启动 android 工作室,使缓存无效,clean/build。但是,我仍然无法从我的设计视图中摆脱这个错误。知道我错过了什么吗?
这是我的风格:
<style name="AppTheme" parent="android:Theme.Holo.Light">
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
<item name="android:textViewStyle">@style/RobotoTextViewStyle</item>
<item name="android:editTextStyle">@style/RobotoEditTextStyle</item>
</style>
<style name="SplashTheme" parent="android:Theme.Holo.Light">
<item name="android:windowBackground">@drawable/splash</item>
</style>
<style name="RobotoTextViewStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">sans-serif</item>
<item name="android:textSize">@dimen/body</item>
<item name="android:textColor">@color/colorBlack</item>
</style>
<style name="RobotoEditTextStyle" parent="android:Widget.EditText">
<item name="android:fontFamily">sans-serif</item>
<item name="android:textSize">@dimen/body</item>
<item name="android:padding">@dimen/edittext_padding</item>
</style>
<style name="CustomTabLayout" parent="CustomTabLayout">
<item name="tabIndicatorColor">@color/colorGold</item>
<item name="tabSelectedTextColor">@color/colorGold</item>
</style>
<style name="StyledScrollerTextAppearance">
<item name="android:textColor">@color/colorWhite</item>
</style>
<style name="CustomSwitch">@style/CustomSwitch</style>
错误如下
java.lang.NullPointerException
at android.text.StaticLayout.<init>(StaticLayout.java:422)
at android.support.v7.widget.SwitchCompat.makeLayout(SwitchCompat.java:864)
at android.support.v7.widget.SwitchCompat.onMeasure_Original(SwitchCompat.java:783)
at android.support.v7.widget.SwitchCompat.onMeasure(SwitchCompat.java)
at android.view.View.measure(View.java:19731)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:927)
at android.support.constraint.ConstraintLayout.onMeasure_Original(ConstraintLayout.java:966)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java)
at android.view.View.measure(View.java:19731)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:927)
at android.support.constraint.ConstraintLayout.onMeasure_Original(ConstraintLayout.java:966)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java)
at android.view.View.measure(View.java:19731)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1293)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.widget.ScrollView.onMeasure(ScrollView.java:340)
at android.view.View.measure(View.java:19731)
at android.support.constraint.ConstraintLayout.onMeasure_Original(ConstraintLayout.java:1021)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java)
at android.view.View.measure(View.java:19731)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19731)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19731)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.measureView(RenderSessionImpl.java:545)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:342)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:389)
at com.android.tools.idea.rendering.RenderTask.compute(RenderTask.java:548)
at com.android.tools.idea.rendering.RenderTask.compute(RenderTask.java:533)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:966)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:533)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate(RenderTask.java:659)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
这是我的部分布局xml
<android.support.constraint.ConstraintLayout
android:id="@+id/saveCopyContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/saveCopyBorderTop">
<TextView
android:id="@+id/saveCopyLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Save a copy"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<android.support.v7.widget.SwitchCompat
android:id="@+id/saveCopySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:checked="true"
android:theme="@style/CustomSwitch"
app:layout_constraintBottom_toBottomOf="@+id/saveCopyLabel"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="@+id/saveCopyLabel"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/saveCopyLabel"/>
</android.support.constraint.ConstraintLayout>
SwitchCompat
需要这两个:
android:textOn
和
android:textOff
所以在你xml变化:
<android.support.v7.widget.SwitchCompat
android:id="@+id/saveCopySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:textOn="On" //add this
android:textOff="Off" //add this
android:checked="true"
android:theme="@style/CustomSwitch"
app:layout_constraintBottom_toBottomOf="@+id/saveCopyLabel"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="@+id/saveCopyLabel"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/saveCopyLabel"/>