RecyclerView滑动出现空指针异常

<span style="font-size:24px;"><strong>异常信息:</strong></span>
05-07 14:29:49.924: E/MessageQueue-JNI(16160): java.lang.NullPointerException
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.support.v7.widget.RecyclerView.computeVerticalScrollOffset(RecyclerView.java:1613)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.View.canScrollVertically(View.java:11380)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.support.v4.view.ViewCompatICS.canScrollVertically(ViewCompatICS.java:35)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.support.v4.view.ViewCompat$ICSViewCompatImpl.canScrollVertically(ViewCompat.java:1253)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.support.v4.view.ViewCompat.canScrollVertically(ViewCompat.java:1695)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.support.v4.widget.SwipeRefreshLayout.canChildScrollUp(SwipeRefreshLayout.java:646)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.support.v4.widget.SwipeRefreshLayout.onInterceptTouchEvent(SwipeRefreshLayout.java:660)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1827)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1904)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1904)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1904)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1904)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1904)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1904)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1968)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1420)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.app.Activity.dispatchTouchEvent(Activity.java:2428)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1916)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.View.dispatchPointerEvent(View.java:7437)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3610)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3538)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4790)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4750)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4902)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.os.MessageQueue.nativePollOnce(Native Method)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.os.MessageQueue.next(MessageQueue.java:125)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.os.Looper.loop(Looper.java:140)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at android.app.ActivityThread.main(ActivityThread.java:5297)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at java.lang.reflect.Method.invokeNative(Native Method)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at java.lang.reflect.Method.invoke(Method.java:511)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-07 14:29:49.924: E/MessageQueue-JNI(16160): 	at dalvik.system.NativeStart.main(Native Method)
05-07 14:29:49.930: E/AndroidRuntime(16160): FATAL EXCEPTION: main
05-07 14:29:49.930: E/AndroidRuntime(16160): java.lang.NullPointerException
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.support.v7.widget.RecyclerView.computeVerticalScrollOffset(RecyclerView.java:1613)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.View.canScrollVertically(View.java:11380)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.support.v4.view.ViewCompatICS.canScrollVertically(ViewCompatICS.java:35)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.support.v4.view.ViewCompat$ICSViewCompatImpl.canScrollVertically(ViewCompat.java:1253)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.support.v4.view.ViewCompat.canScrollVertically(ViewCompat.java:1695)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.support.v4.widget.SwipeRefreshLayout.canChildScrollUp(SwipeRefreshLayout.java:646)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.support.v4.widget.SwipeRefreshLayout.onInterceptTouchEvent(SwipeRefreshLayout.java:660)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1827)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1904)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1904)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1904)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1904)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1904)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1904)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1968)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1420)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.app.Activity.dispatchTouchEvent(Activity.java:2428)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1916)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.View.dispatchPointerEvent(View.java:7437)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3610)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3538)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4790)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4750)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4902)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.os.MessageQueue.nativePollOnce(Native Method)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.os.MessageQueue.next(MessageQueue.java:125)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.os.Looper.loop(Looper.java:140)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at android.app.ActivityThread.main(ActivityThread.java:5297)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at java.lang.reflect.Method.invokeNative(Native Method)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at java.lang.reflect.Method.invoke(Method.java:511)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-07 14:29:49.930: E/AndroidRuntime(16160): 	at dalvik.system.NativeStart.main(Native Method)
05-07 14:29:50.048: E/AppErrorDialog(2309): Failed to get ILowStorageHandle instance
05-07 14:29:50.305: E/AEE/DUMPSTATE(16293): copy_process: execv /system/xbin/showmap failed(2), No such file or directory
05-07 14:29:50.867: E/AEE/DUMPSTATE(16291): copy_file: Copy /proc/gpulog to SYS_GPU_INFO failed(2), No such file or directory
05-07 14:29:52.425: E/AEE/DUMPSTATE(16303): copy_process: execv /system/xbin/procrank failed(2), No such file or directory
05-07 14:29:53.023: E/AEE/DUMPSTATE(16291): copy_file: Copy /proc/slabinfo to SYS_SLAB_INFO failed(2), No such file or directory
05-07 14:29:53.113: E/AEE/DUMPSTATE(16291): copy_file: Copy /sys/kernel/debug/wakeup_sources to SYS_KERNEL_WAKELOCKS failed(2), No such file or directory




CourseActivity.java
private void initView() {
。。。
recyclerView = (RecyclerView) findViewById(R.id.rv_activity_course);
。。。
presenter.initCoursesList();
}
@Override
public void initCoursesList(List<Course> courses) {
    adapter = new CommonRecyclerViewAdapter<Course>(this, R.layout.item_activity_course, courses) {
        @Override
        public void convert(CommondRecyclerViewHolder holder, Course course) {
            //...
        }
    };
    recyclerView.setLayoutManager(new LinearLayoutManager(this));    //关键点
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(adapter);
}
 
  
错误的关键在于:reclerView在初始化数据时由于执行的是耗时操作,而布局管理器又是在耗时操作的后面,导致在数据加载的过程中,还没设置布局管理器,此时就滑动reclerView就会报空指针的错误啦。
解决的方法也很简单,findViewById之后马上就设置布局管理器就可以啦,即:
recyclerView = (RecyclerView) findViewById(R.id.rv_activity_course);
recyclerView.setLayoutManager(new LinearLayoutManager(this));    //关键点
//。。。。。。再做其他事情

你可能感兴趣的:(空指针,RecyclerView)