问题
I have a swipe to refresh layout (part of android.support.v4 revision 19.1), and when I swipe down on the layout I can get to the point where the top part is full, but swiping down any further will cause the app to crash, and the logcat points to no specific line in my code. Since SwipeRefreshLayout is only a couple of months old, their is not a lot of information on the internet to help me figure out what the problem is. Before I was having trouble adding the support v4 revision 19.1 to my build path, but I'm pretty sure I fixed that. Here is the logcat, along with the layout file and the code for my SwipeRefreshLayout. Along with a screenshot of when the app crashes.
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
Here is the layout
<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>
here is the code that deals with 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.
回答1:
Your OnRefreshListener
is null, either you haven't set one or at some point of your code you are setting it to null.
In the source of SwipeRefreshLayout
, particularly in this snippet
private void startRefresh() {
removeCallbacks(mCancel);
mReturnToStartPosition.run();
setRefreshing(true);
mListener.onRefresh();
}
on line 441, the mListener.onRefresh();
raises NullPointerException
.
回答2:
I figured it out. Since this fragment is contained in a viewpager, the SwipeRefreshLayout carries over to the other fragments in the viewpagers, but crashes in the other fragments unless you add the code to it. Really stupid mistake on my part.
来源:https://stackoverflow.com/questions/24298214/exception-dispatching-input-event-and-null-pointer-exception-on-swiperefreshlayo