我在我的应用程序,没有明确的原因得到此错误。没有行有关我的代码,我真的不知道是什么问题,如何解决这个问题。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.afranet.salesportal, PID: 20933
java.lang.NullPointerException: Attempt to invoke virtual method 'float android.view.MotionEvent.getX()' on a null object reference
at android.view.View.onTouchEvent(View.java:10608)
at android.support.design.widget.CoordinatorLayout.onTouchEvent(CoordinatorLayout.java:449)
at android.view.View.dispatchTouchEvent(View.java:9187)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2644)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2351)
at android.support.v4.widget.DrawerLayout.cancelChildViewTouch(DrawerLayout.java:1668)
at android.support.v4.widget.DrawerLayout$ViewDragCallback.peekDrawer(DrawerLayout.java:1916)
at android.support.v4.widget.DrawerLayout$ViewDragCallback.access$000(DrawerLayout.java:1801)
at android.support.v4.widget.DrawerLayout$ViewDragCallback$1.run(DrawerLayout.java:1807)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6917)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
这里是我的布局︰
activity_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorLightGrayBackground"
android:layoutDirection="rtl"
tools:openDrawer="start">
<include
layout="@layout/app_bar_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/colorNavbarBackground"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_home"
app:itemIconTint="@drawable/navigation_icon_color"
app:itemTextColor="@drawable/navigation_icon_color"
app:menu="@menu/activity_home_drawer" />
</android.support.v4.widget.DrawerLayout>
app_bar_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".activity.HomeActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include
layout="@layout/content_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="?attr/actionBarSize" />
<!--android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email"
android:visibility="gone" / !-->
</android.support.design.widget.CoordinatorLayout>
content_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/appbar"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".activity.HomeActivity"
tools:showIn="@layout/app_bar_home">
</RelativeLayout>
它似乎有点错误 CoordinatorLayout
代码在 onTouchEvent
方法。
解决方法 1:
看来这是一个 bug 在支持库 com.android.support:design:23.0.0
。我发现这些问题的答案,解决问题。
1-更新支持库将自动修复问题。
2-重写 dispatchTouchEvent
方法在 activity
将解决该问题︰
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
try {
return super.dispatchTouchEvent(ev);
} catch (Exception e) {
return false;
}
}
3-这个答案也是很好︰
ViewGroup appBarLayout = (ViewGroup) ret.findViewById(R.id.appbarlayout);
for (int i = 0; i < appBarLayout.getChildCount(); i++) {
View childView = appBarLayout.getChildAt(i);
if (!childView.isClickable()) {
childView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return true;
}
});
}
}