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.