当前位置:   article > 正文

android.text.BoringLayout.isBoring 的 NullPointerException

android.text.BoringLayout.isBoring 的 NullPointerException

都是重写TextView.settext()函数导致的坑~

  1. override fun setText(text: CharSequence?, type: BufferType?) {
  2. if (text.isNullOrEmpty()) {
  3. return
  4. }
  5. //业务代码
  6. super.setText(text, type)
  7. }
  1. java.lang.NullPointerException
  2. at android.text.BoringLayout.isBoring(BoringLayout.java:260)
  3. at android.widget.TextView.onMeasure(TextView.java:6322)
  4. at android.view.View.measure(View.java:15473)
  5. at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5056)
  6. at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
  7. at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
  8. at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
  9. at android.view.View.measure(View.java:15473)
  10. at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1223)
  11. at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
  12. at android.view.View.measure(View.java:15473)
  13. at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5056)
  14. at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
  15. at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
  16. at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
  17. at android.view.View.measure(View.java:15473)
  18. at android.widget.ListView.setupChild(ListView.java:1853)
  19. at android.widget.ListView.makeAndAddView(ListView.java:1778)
  20. at android.widget.ListView.fillDown(ListView.java:678)
  21. at android.widget.ListView.fillFromTop(ListView.java:739)
  22. at android.widget.ListView.layoutChildren(ListView.java:1614)
  23. at android.widget.AbsListView.onLayout(AbsListView.java:2296)
  24. at android.view.View.layout(View.java:14055)
  25. at android.view.ViewGroup.layout(ViewGroup.java:4604)
  26. at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
  27. at android.view.View.layout(View.java:14055)
  28. at android.view.ViewGroup.layout(ViewGroup.java:4604)
  29. at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
  30. at android.view.View.layout(View.java:14055)
  31. at android.view.ViewGroup.layout(ViewGroup.java:4604)
  32. at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
  33. at android.view.View.layout(View.java:14055)
  34. at android.view.ViewGroup.layout(ViewGroup.java:4604)
  35. at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1589)
  36. at android.view.View.layout(View.java:14055)
  37. at android.view.ViewGroup.layout(ViewGroup.java:4604)
  38. at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
  39. at android.view.View.layout(View.java:14055)
  40. at android.view.ViewGroup.layout(ViewGroup.java:4604)
  41. at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
  42. at android.view.View.layout(View.java:14055)
  43. at android.view.ViewGroup.layout(ViewGroup.java:4604)
  44. at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
  45. at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
  46. at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
  47. at android.view.View.layout(View.java:14055)
  48. at android.view.ViewGroup.layout(ViewGroup.java:4604)
  49. at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
  50. at android.view.View.layout(View.java:14055)
  51. at android.view.ViewGroup.layout(ViewGroup.java:4604)
  52. at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1992)
  53. at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1813)
  54. at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1112)
  55. at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4472)
  56. at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
  57. at android.view.Choreographer.doCallbacks(Choreographer.java:555)
  58. at android.view.Choreographer.doFrame(Choreographer.java:525)
  59. at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
  60. at android.os.Handler.handleCallback(Handler.java:615)
  61. at android.os.Handler.dispatchMessage(Handler.java:92)
  62. at android.os.Looper.loop(Looper.java:137)
  63. at android.app.ActivityThread.main(ActivityThread.java:4898)
  64. at java.lang.reflect.Method.invokeNative(Native Method)
  65. at java.lang.reflect.Method.invoke(Method.java:511)
  66. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
  67. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
  68. at dalvik.system.NativeStart.main(Native Method)

我自定义了 TextView 目的是去掉自带的内边距

导致报这个错误的地方是在当判断text为空的情况下没有调用super的设置文本函数,只是调用了“return”。这种情况下,'text' 为 null,就会导致上述 NPE

所以可以这么改下

  1. if (text.isNullOrEmpty()) {
  2. super.setText(text, type)
  3. return
  4. }

注意、注意、注意: 如果重写 setText,则必须在调用时设置一个值。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/153086
推荐阅读
相关标签
  

闽ICP备14008679号