Ekwy
Ekwy

Reputation: 11

Android DataBinding : Unable to access LayoutNameDataBinding even if it is generated

My problem is that I need to access my LayoutNameBinding, but I am unable to do so. Only DataBinding generated class which is available is LayoutNameBindingImpl.

The commented part is what I would like to be able to do within my code. Part of the code shown is inspired of https://github.com/android/sunflower which explains how to use different parts of Android JetPack.

Solutions tried

Intellisense Result

LobbyFragment.kt

package com.kotlin.projet3.ui.lobby

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.NavController
import com.kotlin.projet3.R
import com.kotlin.projet3.adapters.GAME_LIST_INDEX
import com.kotlin.projet3.adapters.MATCH_HISTORY_INDEX
import com.kotlin.projet3.adapters.PROFILE_INDEX
import com.kotlin.projet3.databinding.LobbyFragmentBindingImpl

class LobbyFragment : Fragment(){
    private lateinit var nav: NavController

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

//        val binding = LobbyFragmentBinding.inflate(inflater,container,false)
//
//        viewPager = binding.viewPager
//        viewPager.adapter = LobbyTabsAdapter(this)
//
//        TabLayoutMediator(binding.tabs, viewPager) { tab, position ->
//            tab.setIcon(getTabIcon(position))
//            tab.text = getTabTitle(position)
//        }.attach()
//
//        return binding.root
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    private fun getTabIcon(position: Int): Int {
        return when (position) {
            GAME_LIST_INDEX -> R.drawable.ic_home_black_24dp
            PROFILE_INDEX -> R.drawable.ic_person_black_24dp
            MATCH_HISTORY_INDEX -> R.drawable.ic_assessment_black_24dp
            else -> throw IndexOutOfBoundsException()
        }
    }

    private fun getTabTitle(position: Int): String? {
        return when (position) {
            GAME_LIST_INDEX -> getString(R.string.home_button)
            PROFILE_INDEX -> getString(R.string.profile_button)
            MATCH_HISTORY_INDEX -> getString(R.string.match_history_button)
            else -> null
        }
    }
}

lobby_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">


        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/page_container"
            android:layout_width="0dp"
            android:layout_height="match_parent"


            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="parent">

            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintBottom_toTopOf="@+id/view_pager"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_percent=".15"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:tabMode="fixed"
                app:tabGravity="fill"
                app:tabIndicatorFullWidth="true" />

            <androidx.viewpager2.widget.ViewPager2
                android:id="@+id/view_pager"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintTop_toBottomOf="@+id/tabs"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

LobbyFragmentBinding.java (Generated file)

// Generated by data binding compiler. Do not edit!
package com.kotlin.projet3.databinding;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.tabs.TabLayout;
import com.kotlin.projet3.R;
import java.lang.Deprecated;
import java.lang.Object;

public abstract class LobbyFragmentBinding extends ViewDataBinding {
  @NonNull
  public final ConstraintLayout pageContainer;

  @NonNull
  public final TabLayout tabs;

  @NonNull
  public final ViewPager2 viewPager;

  protected LobbyFragmentBinding(Object _bindingComponent, View _root, int _localFieldCount,
      ConstraintLayout pageContainer, TabLayout tabs, ViewPager2 viewPager) {
    super(_bindingComponent, _root, _localFieldCount);
    this.pageContainer = pageContainer;
    this.tabs = tabs;
    this.viewPager = viewPager;
  }

  @NonNull
  public static LobbyFragmentBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable ViewGroup root, boolean attachToRoot) {
    return inflate(inflater, root, attachToRoot, DataBindingUtil.getDefaultComponent());
  }

  /**
   * This method receives DataBindingComponent instance as type Object instead of
   * type DataBindingComponent to avoid causing too many compilation errors if
   * compilation fails for another reason.
   * https://issuetracker.google.com/issues/116541301
   * @Deprecated Use DataBindingUtil.inflate(inflater, R.layout.lobby_fragment, root, attachToRoot, component)
   */
  @NonNull
  @Deprecated
  public static LobbyFragmentBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable ViewGroup root, boolean attachToRoot, @Nullable Object component) {
    return ViewDataBinding.<LobbyFragmentBinding>inflateInternal(inflater, R.layout.lobby_fragment, root, attachToRoot, component);
  }

  @NonNull
  public static LobbyFragmentBinding inflate(@NonNull LayoutInflater inflater) {
    return inflate(inflater, DataBindingUtil.getDefaultComponent());
  }

  /**
   * This method receives DataBindingComponent instance as type Object instead of
   * type DataBindingComponent to avoid causing too many compilation errors if
   * compilation fails for another reason.
   * https://issuetracker.google.com/issues/116541301
   * @Deprecated Use DataBindingUtil.inflate(inflater, R.layout.lobby_fragment, null, false, component)
   */
  @NonNull
  @Deprecated
  public static LobbyFragmentBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable Object component) {
    return ViewDataBinding.<LobbyFragmentBinding>inflateInternal(inflater, R.layout.lobby_fragment, null, false, component);
  }

  public static LobbyFragmentBinding bind(@NonNull View view) {
    return bind(view, DataBindingUtil.getDefaultComponent());
  }

  /**
   * This method receives DataBindingComponent instance as type Object instead of
   * type DataBindingComponent to avoid causing too many compilation errors if
   * compilation fails for another reason.
   * https://issuetracker.google.com/issues/116541301
   * @Deprecated Use DataBindingUtil.bind(view, component)
   */
  @Deprecated
  public static LobbyFragmentBinding bind(@NonNull View view, @Nullable Object component) {
    return (LobbyFragmentBinding)bind(component, view, R.layout.lobby_fragment);
  }
}

Upvotes: 0

Views: 2160

Answers (1)

Ekwy
Ekwy

Reputation: 11

Bug Tracker https://issuetracker.google.com/issues/151140024

Workaround used (a simpler one probably exists, investigation to pinpoint bug was done afterward)

  1. Copy content of lobby_fragment and LobbyFragment
  2. Delete lobby_fragment and LobbyFragment
  3. Create LobbyFragment and lobby_fragment
  4. Paste copied content

After investigation, the problem seems linked to the way layout files are arranged within my project. I defined extra ressource source dirs within my build.gradle(:app) and when I move my layout files to those folders before doing the first build which would create the generated DataBinding files, the issue within this thread appears.

build.gradle(:app)

android {
    dataBinding {
        enabled = true
    }

    sourceSets {
        main {
            res {
                srcDirs 'src/main/res', 
                'src/main/res/layouts/chat',
                'src/main/res/layouts/authentication', 
                'src/main/res/layouts/lobby'
            }
        }
    }

    ...
}

Upvotes: 1

Related Questions