Data Binding "error: Cannot Find Symbol Class Models"
Solution 1:
Okay, I realised where the "class Models does not exist" thing comes from. I don't know whether to blame my own stupidity or the stupidly nitpicky way this binding is implemented on Android. The package needed to be called models
with a lower case "m", not Models
. The binding auto-name-conversion thing must have thought Models
was a class, not a package.
To fix the layout, the onCreate
method had to be changed to
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityMainBindingbinding= DataBindingUtil.setContentView(this, R.layout.activity_main);
dataModel = newDataModel();
cycleInformationBinding.setRecommendation(dataModel);
// set toolbarToolbartoolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Drawer layout settingDrawerLayoutdrawer= (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggletoggle=newActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationViewnavigationView= (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
Specifically, things had to happen in the order:
setContentView
to the main activity- Set up the data model binding
- Layout concerns like drawer and toolbar.
Any other order would cause either the model binding to fail or the toolbar to not display correctly.
Solution 2:
Just convert your existing layouts to data binding layouts (Don't forget to add variable
in your xml with your activity mentioned in type)
Example:
<variable name="navdrawer" type="com.example.sampleapp.HomeScreenActivity" />
This will generate a data binding class for NavigationHeaderView in this format(May differ) NavHeaderYourActivityName.
When you bind your parent activity, you will use that binding instance to get DrawerLayout and your NavigationView, respectively.
A sample code to reduce the boilerplate for initializing views in your code:
NavHeaderHomeScreenBindingnavHeaderHomeScreenBinding=
DataBindingUtil.setContentView(this, R.layout.nav_header_home_screen);
AppBarHomeScreenBindingappBarHomeScreenBinding=
DataBindingUtil.setContentView(this, R.layout.app_bar_home_screen);
ActionBarDrawerToggletoggle=newActionBarDrawerToggle(this, binding.drawerLayout, appBarHomeScreenBinding.toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
binding.drawerLayout.addDrawerListener(toggle);
toggle.syncState();
binding.navView.setNavigationItemSelectedListener(this);
I hope this helps someone! Thank you!
Solution 3:
First try to remove redurant setContentView(R.layout.activity_main);
ActivityMainBindingbinding= DataBindingUtil.setContentView(this, R.layout.activity_main);
Second add to end of "onCreate" function
binding.included.setValues(dataModel);
binding.executePendingBindings();
if you use include than you may add to your include id for example
<include
android:id="@+id/included"
layout="@layout/content_main"
app:values="@{DataModel}"/>
and use
binding.included.setValues(dataModel);
research about using databinding with included layouts
Post a Comment for "Data Binding "error: Cannot Find Symbol Class Models""