'Modification Of addView in customView Android LinearLayout

I Want To Create a View i.e root as LinearLayout also want to add a child view as a new LinearLayout so whenever I call addView() method I want to add my another View to the child view. i am trying to modify override of addview and i did modifcation but i am getting error even in cardView linearLayout and relative layout. i am trying to acheive " One linearLayout as rootView second as childView but root view should add Child view and childview should add otherview like textView, button ,other linearlayout or another view dynamically or programatically by making customView but when i am modifying addView app closes immediately and gives error

For example

[you can view this][1] []: https://i.stack.imgur.com/kgAXk.jpg

But get error like this :
2022-04-12 10:26:40.392 31017-31017/? E/AndroidRuntime:     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7814)
        at android.view.View.resetRtlProperties(View.java:20333)
        at android.view.ViewGroup.addViewInner(ViewGroup.java:5298)
        at android.view.ViewGroup.addView(ViewGroup.java:5076)
2022-04-12 10:26:40.392 31017-31017/? E/AndroidRuntime:     at com.scenariopy.libraryBuilder.views.CustomLayout.addView(CustomLayout.java:39)
        at android.view.ViewGroup.addView(ViewGroup.java:5016)
        at android.view.ViewGroup.addView(ViewGroup.java:4988)
        at com.scenariopy.libraryBuilder.views.CustomLayout.init(CustomLayout.java:29)
        at com.scenariopy.libraryBuilder.views.CustomLayout.<init>(CustomLayout.java:22)

& my code :

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

public class CustomLayout extends LinearLayout {
    LinearLayout op;

    public CustomLayout(Context context) {
        super(context);
        init();
    }

    public CustomLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    void init() {
        op = new LinearLayout(getContext());
        op.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
        op.setBackgroundColor(Color.BLACK);
        super.addView(op);
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
       if (op==null){
           super.addView(child, index, params);
       }
       else
       {
           op.addView(child, index, params);
       }
    }}




Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source