在SwipeRefreshLayout上的异常调度输入事件和空指针异常下拉

时间:2014-06-19 03:18:03

标签: android android-listview nullpointerexception android-support-library 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;

    }

这是一个崩溃的屏幕截图,因为你可以看到它已被一直向下滑动,通常它会触发这一点。但随后崩溃了。

And here is a screenshot of it crashing, as you can see it has been swiped all the way down, and normally it would trigger something at this point. But then it crashes.

2 个答案:

答案 0 :(得分:4)

您的OnRefreshListener为空,要么您没有在代码中设置一个代码,要么将代码设置为null。

SwipeRefreshLayout的来源中,特别是在此代码段中

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

mListener.onRefresh();提升NullPointerException

答案 1 :(得分:0)

我明白了。由于此片段包含在viewpager中,SwipeRefreshLayout会转移到viewpagers中的其他片段,但除非您向其中添加代码,否则会崩溃其他片段。我真的很愚蠢。