0

Following is my layout file : activity_start.xml

    <LinearLayout
    android:id="@+id/ll_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ViewStub
        android:id="@+id/view_stub_main"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_marginBottom="@dimen/dp_8"/>

    </LinearLayout>

This I did in my Activity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);

    ViewStub viewStub = (ViewStub)findViewById(R.id.view_stub_main);
    viewStub.setLayoutResource(R.layout.test_layout);
    View view =  viewStub.inflate();
    }

This is working well. But when I am trying to create the ViewStub dynamically from my Activity without declaring the ViewStub in the xml like :

    ViewStub viewStub = new ViewStub(this,R.layout.test_layout);
    View view = viewStub.inflate();

This is causing the error: java.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent

Where I am doing the wrong!! Thanks in advance.

Mukla.C
  • 53
  • 3
  • 9
  • http://stackoverflow.com/questions/5987682/viewstubparent-viewgroup – Arash GM Jul 20 '14 at 09:27
  • Just curious, but why would you want to create a `ViewStub` programmatically? Wouldn't it be preferable to just inflate the layout and add it? – Mike M. Jul 20 '14 at 09:27

0 Answers0