Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Android Studio fail to build when accessing a ViewStub through ViewDataBinding?

I have a layout defined that contains 2 ViewStubs. They are defined as follows:

<ViewStub
        android:id="@+id/top_divider_stub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inflatedId="@+id/top_divider_layout"
        android:layout="@layout/include_line_separator_horizontal"/>

<ViewStub
        android:id="@+id/bottom_divider_stub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inflatedId="@+id/bottom_divider_layout"
        android:layout="@layout/include_line_separator_horizontal"/>

In my adapter, I'm trying to access the ViewStub through the ViewDataBinding generated by the DataBinding library, in my case it's TitleBinding. Attempting to do the following causes an error.

TitleBinding binding = DataBindingUtil.bind(view);
ViewStub topDividerStub = binding.topDividerStub;
ViewStub bottomDividerStub = binding.bottomDividerStub;

I receive this error and the application fails to compile.

Error:(31, 50) error: incompatible types: ViewStubProxy cannot be converted to ViewStub

I worked around it by setting the variable to type ViewStubProxy, allowing it to compile but giving an incompatible types syntax highlighting error. I'd like to know what the correct way of doing this if possible.

This works...

ViewStubProxy topDividerStub = binding.topDividerStub;
ViewStubProxy bottomDividerStub = binding.bottomDividerStub;

Resulting in

error screenshot

like image 637
SeptimusX75 Avatar asked Nov 09 '22 19:11

SeptimusX75


1 Answers

I have the same problem. It seems this is a bug of DataBinding. I'm not sure.

Or you can create a ViewStubProxy by yourself:

TitleBinding binding = DataBindingUtil.bind(view);

ViewStubProxy viewStubProxy = new ViewStubProxy((ViewStub) view.findViewById(R.id.viewStub));

viewStubProxy.setContainingBinding(binding);
like image 197
chiemy Avatar answered Nov 14 '22 22:11

chiemy