View binding with include layout not working

Tags: , , ,



I want to use view binding to bind the view in include layout. I had given the included layout with an id ‘topBar’ but still failed to access the views inside of it. And I try to use Android studio auto-fix to create a local variable to see its type. It shows type “android.widget . a” I am using Android Studio4.0. Is this a bug in Android Studio?

My Activity

    private ActivityWalletBinding viewBinding;
    private ViewTitleBinding topBarBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding = ActivityWalletBinding.inflate(getLayoutInflater());
        setContentView(viewBinding.getRoot());
        viewBinding.topBar;
        android.widget . a = viewBinding.topBar; //Create local var using auto fix in android 
studio 
}

activity_wallet.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <include
        android:id="@+id/topBar"
        layout="@layout/view_title"/>

view_title.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="65dp"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@color/white"
    android:paddingTop="19dp"
    android:id="@+id/fl_tab">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <ImageView
            android:id="@+id/btn_back"
            android:layout_width="39dp"
            android:layout_height="39dp"
            android:layout_centerVertical="true"
            android:onClick="backClick"
            android:padding="11dp"
            android:src="@mipmap/icon_back"
            android:tint="@color/gray1"
            />
        <TextView
            android:id="@+id/titleView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:textColor="@color/textColor"
            android:textSize="18sp"
            android:textStyle="bold"
            />
        <TextView
            android:id="@+id/tv_right_title"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:layout_alignParentRight="true"
            android:layout_marginRight="11dp"
            android:textColor="@color/enable_right_title"
            android:textSize="15sp"
            tools:text="假文字"
            />
    </RelativeLayout>
</FrameLayout>

Is this a bug for android studio or something?

Answer

I do nothing on it and it just works automatically. I surely did clean, rebuild, restart Android studio… something at that time but not working. Maybe link another layout in another class would trigger something just fix it under the hood cause I set aside this problem and keep going.



Source: stackoverflow