DrawerLayout - 异常调度输入事件

时间:2013-07-26 08:49:37

标签: android android-layout android-fragments android-support-library drawerlayout

我最近在一个星期前开始在一个新项目中使用DrawerLayout,并且一切正常。除了用我的手指轻扫它的奇怪时间,它会崩溃。

如果我使用ActionBar Up / Home MenuItem,它每次都会正常工作。

在LogCat中我得到两个堆栈跟踪,它们似乎都指向内部的android位置,所以我不知道如何解决这个问题。

我只是尝试创建一个新的应用程序,以查看它是否是我之前所做的事情,但它也发生在新的应用程序中。

我在“新建Android应用程序”窗口中使用主/详细信息流模板创建了新应用程序,将所有内容保留为默认值,然后更改了activity_item_list.xml以将其包含在内:

<android.support.v4.widget.DrawerLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/item_list"
        android:name="com.navdrawertest.ItemListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        tools:context=".ItemListActivity"
        tools:layout="@android:layout/list_content" />

    <!-- Navigation Drawer -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111" />
 </android.support.v4.widget.DrawerLayout>

DrawerLayout显示,我仍然可以从屏幕边缘拖动它,但它仍然在此测试应用程序中崩溃。

以下是我用手指拖动后看到的堆栈痕迹:

07-26 09:29:45.574: E/InputEventReceiver(19085): Exception dispatching input event.
07-26 09:29:45.574: E/MessageQueue-JNI(19085): Exception in MessageQueue callback: handleReceiveCallback
07-26 09:29:45.584: E/MessageQueue-JNI(19085): java.lang.NullPointerException
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.support.v4.widget.DrawerLayout.isContentView(DrawerLayout.java:840)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.support.v4.widget.DrawerLayout.onInterceptTouchEvent(DrawerLayout.java:866)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1852)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.View.dispatchPointerEvent(View.java:7564)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5419)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5399)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5370)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5493)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.os.MessageQueue.nativePollOnce(Native Method)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.os.MessageQueue.next(MessageQueue.java:132)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.os.Looper.loop(Looper.java:124)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at android.app.ActivityThread.main(ActivityThread.java:5103)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at java.lang.reflect.Method.invokeNative(Native Method)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at java.lang.reflect.Method.invoke(Method.java:525)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-26 09:29:45.584: E/MessageQueue-JNI(19085):  at dalvik.system.NativeStart.main(Native Method)
07-26 09:29:45.584: D/AndroidRuntime(19085): Shutting down VM
07-26 09:29:45.584: W/dalvikvm(19085): threadid=1: thread exiting with uncaught exception (group=0x41cbb700)
07-26 09:29:45.594: E/AndroidRuntime(19085): FATAL EXCEPTION: main
07-26 09:29:45.594: E/AndroidRuntime(19085): java.lang.NullPointerException
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.support.v4.widget.DrawerLayout.isContentView(DrawerLayout.java:840)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.support.v4.widget.DrawerLayout.onInterceptTouchEvent(DrawerLayout.java:866)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1852)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.View.dispatchPointerEvent(View.java:7564)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5419)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5399)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5370)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5493)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.os.MessageQueue.nativePollOnce(Native Method)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.os.MessageQueue.next(MessageQueue.java:132)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.os.Looper.loop(Looper.java:124)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at android.app.ActivityThread.main(ActivityThread.java:5103)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at java.lang.reflect.Method.invokeNative(Native Method)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at java.lang.reflect.Method.invoke(Method.java:525)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-26 09:29:45.594: E/AndroidRuntime(19085):    at dalvik.system.NativeStart.main(Native Method)

这里有什么我想念的吗?

编辑:10天后仍然无法弄清楚这一点,我能否至少得到确认,这不仅仅是我得到这个?

5 个答案:

答案 0 :(得分:11)

我也收到了此错误消息,但起初我找不到导致它的原因。 在做了一些测试之后,我实际上发现View上的删除所有边距(在你的例子中是片段)似乎可以解决问题。

所以在你的情况下,它将是:

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_list"
    android:name="com.navdrawertest.ItemListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ItemListActivity"
    tools:layout="@android:layout/list_content" />

你可以在这个片段内的所有视图上设置边距(但我还没有测试过它)。 希望它可以帮助某人。看来这个问题并不常见。

答案 1 :(得分:8)

我想我已经设法通过从支持库源代码创建我需要的文件副本并在isContentView()中捕获空视图来解决这个问题。

我需要的来源是:

  • ActionBarDrawerToggle.java
  • ActionBarDrawerToggleHoneycomb.java
  • DrawerLayout.java

我将它们放在我项目中的自己的包中,并将DrawerLayout重命名为FixedDrawerLayout。 (所以我很容易分辨出我正在使用的DrawerLayout的副本)

然后我将isContentView()更改为:

boolean isContentView(View child) {
    if(child == null){
        return false;
    }
    return ((LayoutParams) child.getLayoutParams()).gravity == Gravity.NO_GRAVITY;
}

将我DrawerLayout中的Activity的所有引用交换为FixedDrawerLayout之后,它似乎已经修复了它。

不确定在false中返回isContentView()是否会发生任何事情,但它不再崩溃,即使child为空,它仍然可用。

答案 2 :(得分:4)

基于Mike的回答和user1050133的评论:

1)在您的应用中创建包android.support.v4.widget

2)添加一个名为FixedDrawerLayout的新文件,其中包含以下内容:

public class FixedDrawerLayout extends DrawerLayout {

    public FixedDrawerLayout(Context context) {
        super(context);
    }

    public FixedDrawerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FixedDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    boolean isContentView(View child) {
        if (child == null) {
            return false;
        }
        return ((LayoutParams) child.getLayoutParams()).gravity == Gravity.NO_GRAVITY;
    }
}

3)用FixedDrawerLayout

替换xml中DrawerLayout的所有用法

答案 3 :(得分:2)

我遇到了类似的问题。

我为片段布局添加了一个伪造的onclick mathod

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f1f2f6"
    android:onClick="fragmentClick"
    android:orientation="vertical" >

我把onclick方法放在活动java代码中只是为了假。

public void fragmentClick(View v){
    Log.i("fragmentClick", "fragmentClick");
    return;
}

问题解决了。

答案 4 :(得分:1)

使用导航抽屉时遇到了同样的问题,我只是通过从片段的根布局中删除android:focusableInTouchMode="true"来解决问题。