报错:
原因:因为我的项目需求是在Service弹框(windManager),所以传入的Context是ApplicationContext,导致找不到Theme,所以找不到默认textOff、textOn,所以报空指针异常。
我将同样的代码放在Activity里面就不会。
解决:在xml布局里面,将textOff、textOn加上就行了。
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switchDischarge"
android:layout_width="@dimen/dp74"
android:layout_height="@dimen/dp40"
android:layout_marginStart="@dimen/dp24"
style="@style/SwitchCompatStyle"
android:text=""
android:textOff=""
android:textOn=""/>