gpt4 book ai didi

android - CoordinatorLayout 中的 NullPointerException onTouchEvent

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:49 24 4
gpt4 key购买 nike

我的应用中出现此错误,原因不明。没有一行与我的代码相关,我真的不知道问题出在哪里以及如何解决。

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>

onTouchEvent 方法中的 CoordinatorLayout 代码似乎有问题。

最佳答案

好像是a bug in Support Library com.android.support:design:23.0.0。我找到了解决问题的答案。

1- 更新支持库会自动解决问题。

2- 覆盖 Activity 中的 dispatchTouchEvent 方法将解决问题:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
try {
return super.dispatchTouchEvent(ev);
} catch (Exception e) {
return false;
}
}

3- This answer也不错 :

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;
}
});
}
}

关于android - CoordinatorLayout 中的 NullPointerException onTouchEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37693974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com