美文网首页
动态添加SwitchCompat遇到的坑

动态添加SwitchCompat遇到的坑

作者: InvictusGar | 来源:发表于2018-12-24 19:55 被阅读0次

    项目中遇到需要向layout中动态添加SwitchCompat等一些View,结果添加后报了这个NPE

    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:466)
            at android.support.v7.widget.SwitchCompat.makeLayout(SwitchCompat.java:876)
            at android.support.v7.widget.SwitchCompat.onMeasure(SwitchCompat.java:795)
            at android.view.View.measure(View.java:23169)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
            at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1187)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:706)
            at android.view.View.measure(View.java:23169)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
            at android.view.View.measure(View.java:23169)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
            at android.view.View.measure(View.java:23169)
            at android.support.v4.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1450)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
            at android.support.v4.widget.NestedScrollView.onMeasure(NestedScrollView.java:516)
            at android.view.View.measure(View.java:23169)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
            at android.view.View.measure(View.java:23169)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
            at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
            at android.view.View.measure(View.java:23169)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
            at android.view.View.measure(View.java:23169)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
            at android.view.View.measure(View.java:23169)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
            at android.view.View.measure(View.java:23169)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
            at android.view.View.measure(View.java:23169)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
            at com.android.internal.policy.DecorView.onMeasure(DecorView.java:716)
            at android.view.View.measure(View.java:23169)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2718)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1572)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1855)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
            at android.view.Choreographer.doCallbacks(Choreographer.java:761)
            at android.view.Choreographer.doFrame(Choreographer.java:696)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
            at android.os.Handler.handleCallback(Handler.java:873)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:6680)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
    

    查了很久,Stack Overflow上面看到有说是SwitchCompat的textOn和textOff属性没有设置,报空;

    添加后确实不crash了,不过switch按钮格式也被修改了

    后来找到是这个原因:传入了Application的context,而不是当前Activity的,修改后解决问题。
    https://stackoverflow.com/questions/37992137/adding-switchcompat-programmatically-error

    同时学习了下Rxjava的时序:
    https://www.jianshu.com/p/59c3d6bb6a6b

    相关文章

      网友评论

          本文标题:动态添加SwitchCompat遇到的坑

          本文链接:https://www.haomeiwen.com/subject/hqctlqtx.html