android上的SwitchCompat渲染问题

时间:2017-02-23 20:04:47

标签: android switchcompat

我在我的一个布局中使用android.support.v7.widget.SwitchCompat。 我的编译sdk版本是25,buildTools版本是25.0.1,我使用的是25.2.0版本的com.android.support:design 我重新启动了android studio,使缓存无效,清理/构建。但是,我仍然无法从我的设计视图中消除此错误。知道我错过了什么吗?

这是我的风格:

    <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$2.compute(RenderTask.java:548)
at com.android.tools.idea.rendering.RenderTask$2.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$70(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>

1 个答案:

答案 0 :(得分:1)

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"/>