赞
踩
都是重写TextView.settext()函数导致的坑~
- override fun setText(text: CharSequence?, type: BufferType?) {
- if (text.isNullOrEmpty()) {
- return
- }
- //业务代码
- super.setText(text, type)
- }
- java.lang.NullPointerException
- at android.text.BoringLayout.isBoring(BoringLayout.java:260)
- at android.widget.TextView.onMeasure(TextView.java:6322)
- at android.view.View.measure(View.java:15473)
- at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5056)
- at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
- at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
- at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
- at android.view.View.measure(View.java:15473)
- at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1223)
- at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
- at android.view.View.measure(View.java:15473)
- at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5056)
- at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
- at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
- at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
- at android.view.View.measure(View.java:15473)
- at android.widget.ListView.setupChild(ListView.java:1853)
- at android.widget.ListView.makeAndAddView(ListView.java:1778)
- at android.widget.ListView.fillDown(ListView.java:678)
- at android.widget.ListView.fillFromTop(ListView.java:739)
- at android.widget.ListView.layoutChildren(ListView.java:1614)
- at android.widget.AbsListView.onLayout(AbsListView.java:2296)
- at android.view.View.layout(View.java:14055)
- at android.view.ViewGroup.layout(ViewGroup.java:4604)
- at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
- at android.view.View.layout(View.java:14055)
- at android.view.ViewGroup.layout(ViewGroup.java:4604)
- at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
- at android.view.View.layout(View.java:14055)
- at android.view.ViewGroup.layout(ViewGroup.java:4604)
- at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
- at android.view.View.layout(View.java:14055)
- at android.view.ViewGroup.layout(ViewGroup.java:4604)
- at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1589)
- at android.view.View.layout(View.java:14055)
- at android.view.ViewGroup.layout(ViewGroup.java:4604)
- at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
- at android.view.View.layout(View.java:14055)
- at android.view.ViewGroup.layout(ViewGroup.java:4604)
- at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
- at android.view.View.layout(View.java:14055)
- at android.view.ViewGroup.layout(ViewGroup.java:4604)
- at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
- at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
- at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
- at android.view.View.layout(View.java:14055)
- at android.view.ViewGroup.layout(ViewGroup.java:4604)
- at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
- at android.view.View.layout(View.java:14055)
- at android.view.ViewGroup.layout(ViewGroup.java:4604)
- at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1992)
- at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1813)
- at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1112)
- at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4472)
- at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
- at android.view.Choreographer.doCallbacks(Choreographer.java:555)
- at android.view.Choreographer.doFrame(Choreographer.java:525)
- at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
- at android.os.Handler.handleCallback(Handler.java:615)
- at android.os.Handler.dispatchMessage(Handler.java:92)
- at android.os.Looper.loop(Looper.java:137)
- at android.app.ActivityThread.main(ActivityThread.java:4898)
- at java.lang.reflect.Method.invokeNative(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:511)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
- at dalvik.system.NativeStart.main(Native Method)
我自定义了 TextView 目的是去掉自带的内边距。
导致报这个错误的地方是在当判断text为空的情况下没有调用super的设置文本函数,只是调用了“return”。这种情况下,'text' 为 null,就会导致上述 NPE。
所以可以这么改下
- if (text.isNullOrEmpty()) {
- super.setText(text, type)
- return
- }
注意、注意、注意: 如果重写 setText,则必须在调用时设置一个值。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。