在布局期间,View.getChildAt(i)返回null,即使该位置的视图应该存在

时间:2014-12-18 14:35:03

标签: android android-layout

我得到的NPE所有执行的代码都只是框架代码。也许有人可以给我任何关于我的代码中要注意什么的线索?在哪个活动的生命周期阶段运行这个框架代码?

这在应用程序启动后很长一段时间内发生,所以也许这里正在重新创建应用程序并正在恢复视图?

的FrameLayout:

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    final int count = getChildCount();

    // ...

    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);

        // NPE happens at this line
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();

            final int width = child.getMeasuredWidth();
            final int height = child.getMeasuredHeight();

            // ...

            child.layout(childLeft, childTop, childLeft + width, childTop + height);
        }
    }
}

堆栈跟踪:

java.lang.NullPointerException
    at android.widget.FrameLayout.onLayout(FrameLayout.java:400)
    at android.view.View.layout(View.java:14842)
    at android.view.ViewGroup.layout(ViewGroup.java:4601)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:14842)
    at android.view.ViewGroup.layout(ViewGroup.java:4601)
    at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:887)
    at android.view.View.layout(View.java:14842)
    at android.view.ViewGroup.layout(ViewGroup.java:4601)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:14842)
    at android.view.ViewGroup.layout(ViewGroup.java:4601)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1669)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
    at android.view.View.layout(View.java:14842)
    at android.view.ViewGroup.layout(ViewGroup.java:4601)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:14842)
    at android.view.ViewGroup.layout(ViewGroup.java:4601)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2206)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2020)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1230)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5093)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
    at android.view.Choreographer.doCallbacks(Choreographer.java:591)
    at android.view.Choreographer.doFrame(Choreographer.java:561)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5283)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
    at dalvik.system.NativeStart.main(Native Method)

主要布局:

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

    <!-- Fragment container, may hold various fragments -->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <!-- Fragment container -->
    <!-- Fragment's top view is TabHost which is descendant from FrameLayout -->
    <FrameLayout
        android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="left"/>

    <!-- Fragment container -->
    <!-- Fragment's top view is TabHost which is descendant from FrameLayout -->
    <FrameLayout
        android:id="@+id/personal_drawer"
        android:layout_width="@dimen/personal_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="right"/>

</android.support.v4.widget.DrawerLayout>

编辑:

另一个想法 - 我的一个片段放在@+id/personal_drawer后面有onDestroyView()覆盖:

@Override public void onDestroyView() {
    for (ProductList v : tabsContent) {
        if (v != null) {
            v.tearDown();
        }
    }
    tabsContent = null;
    tabHost = null;
    super.onDestroyView();
}

这可能是个问题吗?

0 个答案:

没有答案