android-SwipeRefreshLayout上的异常调度输入事件和空指针异常

我可以滑动以刷新布局(属于android.support.v4版本19.1的一部分),当我向下滑动布局时,可以到达顶部已满的位置,但是向下滑动则将导致应用程序崩溃,logcat指向我的代码中没有特定的行.由于SwipeRefreshLayout才使用了几个月,因此在互联网上没有太多信息可以帮助我弄清楚问题出在哪里.在向我的构建路径添加支持v4修订版19.1遇到麻烦之前,但是我敢肯定我已解决了该问题.这是logcat,以及布局文件和SwipeRefreshLayout的代码.以及应用程序崩溃时的屏幕截图.

06-18 23:09:31.527: E/InputEventReceiver(25459): Exception dispatching input event.
06-18 23:09:31.547: E/AndroidRuntime(25459): FATAL EXCEPTION: main
06-18 23:09:31.547: E/AndroidRuntime(25459): Process: com.bernard.beaconportal, PID: 25459
06-18 23:09:31.547: E/AndroidRuntime(25459): java.lang.NullPointerException
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.support.v4.widget.SwipeRefreshLayout.startRefresh(SwipeRefreshLayout.java:441)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.support.v4.widget.SwipeRefreshLayout.onTouchEvent(SwipeRefreshLayout.java:399)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.View.dispatchTouchEvent(View.java:7706)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2339)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1569)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.app.Activity.dispatchTouchEvent(Activity.java:2492)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2286)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.View.dispatchPointerEvent(View.java:7886)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3947)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3518)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3575)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5532)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5512)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5483)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5612)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5585)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5631)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
06-18 23:09:31.547: E/AndroidRuntime(25459):    at android.view.Choreographer.doCa

这是布局

<android.support.v4.widget.SwipeRefreshLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:id="@+id/swipe" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
         android:background="#ffffff">

    </ListView>
</android.support.v4.widget.SwipeRefreshLayout>

这是处理SwipeRefreshLayout的代码

SwipeRefreshLayout swipeLayout; 


    public static final String KEY_HOMEWORK="homework";
    public static final String KEY_DESC="desc";

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){


         View swipe = inflater.inflate(R.layout.activity_main, container, false); 

         swipeLayout = (SwipeRefreshLayout) swipe.findViewById(R.id.swipe);

         swipeLayout.setEnabled(false);

    ListView lView = (ListView) swipe.findViewById(R.id.listView1);

    lView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView absListView, int i) {

        }

        @Override
        public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem == 0)
                    swipeLayout.setEnabled(true);
                else
                    swipeLayout.setEnabled(false);
        }
    });


    return swipe;

    }

这是它崩溃的屏幕截图,您可以看到它一直被向下滑动,并且通常它会在此时触发某些操作.但随后崩溃.

解决方法:

您的OnRefreshListener为null,或者您尚未设置一个,或者在代码的某个时候将其设置为null.

在SwipeRefreshLayout的源代码中,尤其是在此片段中

 private void startRefresh() {
        removeCallbacks(mCancel);
        mReturnToStartPosition.run();
        setRefreshing(true);
        mListener.onRefresh();
    }

在第441行,mListener.onRefresh();引发NullPointerException.

上一篇:java-尝试捕获块中的空指针异常


下一篇:java-我怎么知道空指针异常的起源?