ViewStub must have a non-empty ViewGroup viewParent

I use ViewStubsto load the displayed data into my layout. Since I use ButterKnifeto link layout components, I have custom classes that contain separate viewstub layout components, for example, one such viewstub looks like this.

 <ViewStub
      android:id="@+id/featuredContentStub"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:inflatedId="@+id/featuredContent"
      android:layout="@layout/featured_content" />

The class for processing components is @layout/featured_contentas follows:

public class FeaturedContentView {
        @BindView(R.id.art)
        ImageView art;
        @BindView(R.id.shade)
        View shade;
        @BindView(R.id.title)
        TextView featuredTitle;
        @BindView(R.id.performer)
        TextView performer;
        @BindView(R.id.featured_title)
        KerningTextView featuredText;
        @BindView(R.id.play_button)
        Button play;
        @BindView(R.id.shareText)
        TextView shareText;
        @BindView(R.id.addToFavorites)
        ImageView addToFavs;

        FeaturedContentView(View view) {
            ButterKnife.bind(this, view);
        }
    }

I inflate the layout as follows:

if (viewstub != null) {
        View view = viewstub.inflate();
        featuredContentView = new FeaturedContentView(view);
}

The above method is called in two different places in my fragment. It is correctly pumped up for the first time, but the second time does not refer to java.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent.

How can I handle this situation.

+4
source share
1

Android ViewStub :

  • ViewStub ,
  • , inflate.

, , ViewStub .

, ViewStub , ButerKnife. , , , (, isInflated, addInflateCallback ..). Android , .

+3

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


All Articles