今天为试着做一个软件管理的app,然后就为listview加入了OnScrollListener,发现会提示如下错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rentalphang/com.example.rentalphang.activity.AppManagerActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setOnScrollListener(android.widget.AbsListView$OnScrollListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2356)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5289)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setOnScrollListener(android.widget.AbsListView$OnScrollListener)' on a null object reference
com.example.rentalphang.activity.AppManagerActivity.initUI(AppManagerActivity.java:249)
com.example.rentalphang.activity.AppManagerActivity.onCreate(AppManagerActivity.java:57)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5289)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
下意识谷歌,然后没没找到满意答案。然后只能自己琢磨,listview.onscrollistener空对象,就很可能是listview出了问题,然后就想会不会没有findviewbyid吧,麻鸡,一找发现还真是,麻鸡。写这篇博文,也是希望也有这个问题的朋友解决问题,我想不会有吧,犯傻了。还有找BUG我以后还是先自己想一下怎样解决,挺能锻炼人的。想不出再百度。