I am working on an existing project which uses DrawerLayout and a custom fragment for side navigation, but when I use data binding it gives inflate exception in XML file at <fragment> tag. Below is my code.
activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
android:id="@+id/layoutParent"
layout="@layout/activity_parent_list_new" />
<fragment
android:id="@+id/navigation_view"
android:name="com.myApp.home.fragment.NavigationDrawerFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
</androidx.drawerlayout.widget.DrawerLayout>
</layout>
HomeActivity.java
public class HomeActivity extends AppCompatActivity{
private ActivityHomeBinding binding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityHomeBinding.inflate(getLayoutInflater());
mExitToast = Toast.makeText(this, getString(R.string.press_to_exit), Toast.LENGTH_SHORT);
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_view);
mNavigationDrawerFragment.setupViews();
mNavigationDrawerFragment.setDrawerListener(this);
}
/*Other Code*/
}
Exception stack trace
Process: com.myApp.debug, PID: 18493
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myApp.debug/com.myApp.home.HomeActivity}: android.view.InflateException: Binary XML file line #27 in com.myApp.debug:layout/activity_home: Binary XML file line #27 in com.myApp.debug:layout/activity_home: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3780)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3942)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:109)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2345)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:233)
at android.os.Looper.loop(Looper.java:344)
at android.app.ActivityThread.main(ActivityThread.java:8212)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)
Caused by: android.view.InflateException: Binary XML file line #27 in com.myApp.debug:layout/activity_home: Binary XML file line #27 in com.myApp.debug:layout/activity_home: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #27 in com.myApp.debug:layout/activity_home: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #27: Duplicate id 0x7f0a0682, tag null, or parent id 0x7f0a02e9 with another fragment for com.myApp.home.fragment.NavigationDrawerFragment
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:117)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:295)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:274)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1078)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:970)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1132)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1093)
at android.view.LayoutInflater.inflate(LayoutInflater.java:692)
at android.view.LayoutInflater.inflate(LayoutInflater.java:542)
at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:126)
at androidx.databinding.ViewDataBinding.inflateInternal(ViewDataBinding.java:1409)
at com.myApp.databinding.ActivityHomeBinding.inflate(ActivityHomeBinding.java:72)
at com.myApp.databinding.ActivityHomeBinding.inflate(ActivityHomeBinding.java:58)
at com.myApp.home.HomeActivity.onCreate(HomeActivity.java:118)
at android.app.Activity.performCreate(Activity.java:8129)
at android.app.Activity.performCreate(Activity.java:8109)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1344)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3749)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3942)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:109)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2345)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:233)
PS: Code is working fine without Databinding, but when I try to use data binding it gives this error.
Make sure you have correctly implemented data binding in your project by following the instructions in the Android documentation: https://developer.android.com/topic/libraries/data-binding/start
In your XML layout file, remove the tag for your custom fragment and replace it with a tag. This will allow you to include the layout for your fragment within the DrawerLayout, without using a fragment tag.
In your Java code, use the FragmentManager to add your fragment to the DrawerLayout programmatically. This will allow you to use data binding with your custom fragment without causing conflicts.
Make sure you are using the correct data binding syntax in your XML layout file. For example, if you are using a ViewModel to manage the data for your fragment, you should use tags to define the variables in your layout and use the bind: namespace to access them in your layout.