During layout, View.getChildAt (i) returns null, even if the view must exist at this position

I get NPE where all the executable code is just the framework code. Maybe someone can give me any guidance as to what should be observed in my code? And at what stage in the activity life cycle does this framework code execute?

This happens quite a while after the application starts, so maybe the application is recreated here and the viewing is restored?

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

Stacktrace:

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)

The main layout:

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

Edit:

Another idea - one of my snippets that fit in @+id/personal_drawerhas the following onDestroyView()override:

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

Perhaps this is a problem?

+4
source share

Source: https://habr.com/ru/post/1568166/


All Articles