问题
I am getting NullPointerException
in onTouchEvent
of CoordinatorLayout
.
I am using DrawerLayout
with CollapsingToolbarLayout
of android support AppCompat v23.0.0 library.
My observation is crash happen randomly while scrolling CollapsingToolbarLayout
or click on drawer items.
Is there anyone facing this issue? any work around to avoid this exception?
Logcat:
08-22 15:16:34.657: E/AndroidRuntime(27596): java.lang.NullPointerException: Attempt to invoke virtual method 'float android.view.MotionEvent.getX()' on a null object reference
08-22 15:16:34.657: E/AndroidRuntime(27596): at android.view.View.onTouchEvent(View.java:9321)
08-22 15:16:34.657: E/AndroidRuntime(27596): at android.support.design.widget.CoordinatorLayout.onTouchEvent(CoordinatorLayout.java:449)
08-22 15:16:34.657: E/AndroidRuntime(27596): at android.view.View.dispatchTouchEvent(View.java:8388)
08-22 15:16:34.657: E/AndroidRuntime(27596): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2398)
08-22 15:16:34.657: E/AndroidRuntime(27596): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2158)
08-22 15:16:34.657: E/AndroidRuntime(27596): at android.support.v4.widget.DrawerLayout.cancelChildViewTouch(DrawerLayout.java:1491)
08-22 15:16:34.657: E/AndroidRuntime(27596): at android.support.v4.widget.DrawerLayout$ViewDragCallback.peekDrawer(DrawerLayout.java:1739)
08-22 15:16:34.657: E/AndroidRuntime(27596): at android.support.v4.widget.DrawerLayout$ViewDragCallback.access$000(DrawerLayout.java:1624)
08-22 15:16:34.657: E/AndroidRuntime(27596): at android.support.v4.widget.DrawerLayout$ViewDragCallback$1.run(DrawerLayout.java:1630)
08-22 15:16:34.657: E/AndroidRuntime(27596): at android.os.Handler.handleCallback(Handler.java:739)
08-22 15:16:34.657: E/AndroidRuntime(27596): at android.os.Handler.dispatchMessage(Handler.java:95)
08-22 15:16:34.657: E/AndroidRuntime(27596): at android.os.Looper.loop(Looper.java:135)
08-22 15:16:34.657: E/AndroidRuntime(27596): at android.app.ActivityThread.main(ActivityThread.java:5221)
08-22 15:16:34.657: E/AndroidRuntime(27596): at java.lang.reflect.Method.invoke(Native Method)
08-22 15:16:34.657: E/AndroidRuntime(27596): at java.lang.reflect.Method.invoke(Method.java:372)
08-22 15:16:34.657: E/AndroidRuntime(27596): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
08-22 15:16:34.657: E/AndroidRuntime(27596): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
08-22 15:16:35.965: I/Process(27596): Sending signal. PID: 27596 SIG: 9
回答1:
According to the link posed by Kevin Salazar, the bug is fixed if you update your support library to version 23.0.1
回答2:
This works for me. https://code.google.com/p/android/issues/detail?id=183166
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
try {
return super.dispatchTouchEvent(ev);
} catch (Exception e) {
return false;
}
}
回答3:
As David Chu correctly explains, the issue is resolved in versions 23.0.1 and upwards. Also, in my case, Kevin Salazar's suggestion of try-catching the dispatchTouchEvent seemed to lower (could be perception bias) the number of crashes but not completely avoid them.
A key aspect is that the crash is not as random as it appears at first. In my case it occurs when you SLOWLY slide the drawer open. Quickly opening and closing the drawer does not result in a crash. This was helpful for me to reproduce the bug.
来源:https://stackoverflow.com/questions/32154917/coordinatorlayout-nullpointerexception-in-ontouchevent