11

大约一周前,我最近开始在一个新项目中使用 DrawerLayout,一切正常。除了用我的手指刷入和刷出的奇怪时间之外,它会崩溃。

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

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

我只是尝试创建一个新应用程序,看看是否是我之前正在做的事情导致了这种情况,但它也发生在新应用程序中。

我在“新 Android 应用程序”窗口中使用 Master/Detail Flow 模板创建了新应用程序,将所有内容保留为默认值,并更改了 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天后仍然无法弄清楚,我至少可以确认这不仅仅是我得到这个吗?

4

5 回答 5

11

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

因此,在您的情况下,它将是:

<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" />

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

于 2013-09-11T19:23:20.427 回答
8

我想我已经设法通过从支持库源代码创建我自己需要的文件副本并在isContentView().

我需要的源文件是:

  • ActionBarDrawerToggle.java
  • ActionBarDrawerToggleHoneycomb.java
  • 抽屉布局.java

我将它们放在我项目中自己的包中并重命名DrawerLayoutFixedDrawerLayout. (所以我可以很容易地知道我正在使用哪个 DrawerLayout 副本)

然后我改为isContentView()

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

DrawerLayout在交换了对 my Activityto的所有引用之后,FixedDrawerLayout它似乎已经修复了它。

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

于 2013-08-07T15:49:38.547 回答
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 的所有用途

于 2014-04-22T06:13:12.127 回答
2

我遇到了类似的问题。

我为片段布局设置了一个假的 onclick 方法

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

问题解决了。

于 2014-01-01T13:50:14.177 回答
1

我在使用抽屉式导航时遇到了同样的问题,我只需android:focusableInTouchMode="true"从片段的根布局中删除 就解决了这个问题。

于 2013-10-25T08:46:11.563 回答