ListView優化時,控制元件行,報空指標。
阿新 • • 發佈:2018-11-05
FATAL EXCEPTION: main
Process: com.example.wenwenzhang.uicustomviews, PID: 29412
java.lang.NullPointerException: Attempt to write to field 'android.widget.ImageView com.example.wenwenzhang.uicustomviews.FruitAdapter$ViewHolder.fruitImage' on a null object reference
at com.example.wenwenzhang.uicustomviews.FruitAdapter.getView(FruitAdapter.java:35)
at android.widget.AbsListView.obtainView(AbsListView.java:2413)
at android.widget.ListView.makeAndAddView(ListView.java:1986)
at android.widget.ListView.fillDown(ListView.java:709)
at android.widget.ListView.fillFromTop(ListView.java:770)
at android.widget.ListView.layoutChildren(ListView.java:1749)
at android.widget.AbsListView.onLayout(AbsListView.java:2212)
at android.view.View.layout(View.java:17708)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1766)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1610)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1519)
at android.view.View.layout(View.java:17708)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17708)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
at android.view.View.layout(View.java:17708)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17708)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1766)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1610)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1519)
at android.view.View.layout(View.java:17708)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:774)
at android.view.View.layout(View.java:17708)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2519)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2234)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1372)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6782)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:933)
at android.view.Choreographer.doCallbacks(Choreographer.java:742)
at android.view.Choreographer.doFrame(Choreographer.java:674)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:919)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6623)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
Process: com.example.wenwenzhang.uicustomviews, PID: 29412
java.lang.NullPointerException: Attempt to write to field 'android.widget.ImageView com.example.wenwenzhang.uicustomviews.FruitAdapter$ViewHolder.fruitImage' on a null object reference
at com.example.wenwenzhang.uicustomviews.FruitAdapter.getView(FruitAdapter.java:35)
at android.widget.AbsListView.obtainView(AbsListView.java:2413)
at android.widget.ListView.makeAndAddView(ListView.java:1986)
at android.widget.ListView.fillDown(ListView.java:709)
at android.widget.ListView.fillFromTop(ListView.java:770)
at android.widget.ListView.layoutChildren(ListView.java:1749)
at android.widget.AbsListView.onLayout(AbsListView.java:2212)
at android.view.View.layout(View.java:17708)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1766)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1610)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1519)
at android.view.View.layout(View.java:17708)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17708)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
at android.view.View.layout(View.java:17708)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17708)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1766)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1610)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1519)
at android.view.View.layout(View.java:17708)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:774)
at android.view.View.layout(View.java:17708)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2519)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2234)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1372)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6782)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:933)
at android.view.Choreographer.doCallbacks(Choreographer.java:742)
at android.view.Choreographer.doFrame(Choreographer.java:674)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:919)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6623)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
解決問題:ViewHolder例項化不能為空,缺少程式碼viewHolder = new ViewHolder();
順帶提一嘴ListView優化:
不重複載入佈局:在getView()方法裡進行判斷,如果convertView為空,則使用LayoutInflater去載入佈局。不為空,直接對convertView進行重用。大大提高ListView執行效率,快速滾動時表現出更好的效能。
不重新載入控制元件:在上一步的基礎上,使用ViewHolder類,對控制元件的例項進行快取;view.setTag(viewHolder),將ViewHolder物件儲存在view中。convertView不為空的時候呼叫View的getTag()方法,把ViewHolder重新取出。