想把一个老项目里把老掉牙的checkBox 换成原生的SwithCompat,布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context="com.dhl.switchcompatdemo.MainActivity">
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
很简单的一个布局,但是运行起来,却崩溃!???
崩溃如下:
java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference
at android.text.StaticLayout.<init>(StaticLayout.java:422)
at android.support.v7.widget.SwitchCompat.makeLayout(SwitchCompat.java:602)
at android.support.v7.widget.SwitchCompat.onMeasure(SwitchCompat.java:522)
at android.view.View.measure(View.java:19923)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19923)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1489)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:775)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:657)
at android.view.View.measure(View.java:19923)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1489)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:775)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:657)
at android.view.View.measure(View.java:19923)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1489)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:775)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:657)
at android.view.View.measure(View.java:19923)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1489)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:775)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:657)
at android.view.View.measure(View.java:19923)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1330)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.widget.ScrollView.onMeasure(ScrollView.java:352)
at android.view.View.measure(View.java:19923)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19923)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19923)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1489)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:775)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:657)
at android.view.View.measure(View.java:19923)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:729)
at android.view.View.measure(View.java:19923)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2436)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1482)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1741)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1366)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6773)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:926)
at android.view.Choreographer.doCallbacks(Choreographer.java:735)
at android.view.Choreographer.doFrame(Choreographer.java:667)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:912)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:199)
at android.app.ActivityThread.main(ActivityThread.java:6585)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:986)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)
真是莫名其妙,这到底是为何呢?
原来这个老项目的Activity 的主题引用竟然是:
android:theme="@android:style/Theme.DeviceDefault.DialogWhenLarge"
但是这个SwitchCompat 是V7 包才有的,所以引用的主题必须是 Theme.AppCompat 的子类才行,修改如下:
android:theme="@style/AppTheme"
解决。