1

View Binding got released with v3.6.

https://developer.android.com/topic/libraries/view-binding

I am going to use view binding with included layouts.

So I refer to the following answer that is useful.

How to use View Binding with Included Views?

But it doesn't work well.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());

        setContentView(binding.getRoot());

        mainBinding.myHeader.foo.setText("this is a test");
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/my_header"
        layout="@layout/item_header"
        android:layout_width="match_parent"
        android:layout_height="100dp" />

</LinearLayout>

However, the following error occurs.

2020-04-21 10:18:25.135 30977-30977/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 16941
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException: Missing required view with ID: itemId
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
    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:2016)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
 Caused by: java.lang.NullPointerException: Missing required view with ID: itemId
    at com.example.myapplication.databinding.ItemHeaderBinding.bind(ItemHeaderBinding.java:73)
    at com.example.myapplication.databinding.ActivityMainBinding.bind(ActivityMainBinding.java:60)
    at com.example.myapplication.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:46)
    at com.example.myapplication.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:36)
    at com.example.myapplication.MainActivity.onCreate(MainActivity.java:13)
    at android.app.Activity.performCreate(Activity.java:7825)
    at android.app.Activity.performCreate(Activity.java:7814)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
    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:2016) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:214) 
    at android.app.ActivityThread.main(ActivityThread.java:7356) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

What's wrong?

If I remove id of include layout, there are other errors.

ATP
  • 2,939
  • 4
  • 13
  • 34
bigant02
  • 176
  • 3
  • 16
  • check this https://stackoverflow.com/questions/58730127/viewbinding-how-to-get-binding-for-included-layouts – Ravi Apr 21 '20 at 07:00
  • Does this answer your question? [ViewBinding - how to get binding for included layouts?](https://stackoverflow.com/questions/58730127/viewbinding-how-to-get-binding-for-included-layouts) – Ravi Apr 21 '20 at 07:00
  • Yes, I already check it. I implement in first method, but it don't work well. I wrote it down in Java. – bigant02 Apr 21 '20 at 07:11
  • I made a new project and implement it as above, but there is no problem. But my previous project still has the problem. – bigant02 Apr 22 '20 at 01:16

2 Answers2

1

I have resolved the problem.

Original item_header.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="false"
    android:background="@android:color/transparent">
    <TextView
        android:id="@+id/foo"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</FrameLayout>

I removed id of framelayout and the error was soon gone.

<?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="match_parent"
    android:clickable="false"
    android:background="@android:color/transparent">
    <TextView
        android:id="@+id/foo"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</FrameLayout>

Be careful about using id while using view binding.

bigant02
  • 176
  • 3
  • 16
0

This is how I handled included layouts with view binding.

// When view binding is enabled it will create a binding class for each layout, converting the file name to Pascal case. 
ActivityMainBinding binding;
IncludedLayout bindingIncludedLayout;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Inflate the layout as per google doc instructions
    binding = ActivityMainBinding.inflate(getLayoutInflater());

    // add the inflated view to the Included view. 
    bindingIncludedLayout = binding;

    setContentView(binding.getRoot());

    // binding in the main layout
    binding.myView.setText("text");

    // binding in the included layout
    bindingIncludedLayout.textview_name.("additional text")

    }

I would strongly recommend learning data binding over view binding, as data binding has a lot more potential, its like view binding on steroids.

This was the best tutorial I came across for learning data binding.

https://www.androidhive.info/android-working-with-databinding/

Its a few years old now but still very helpful in getting you started.

Shawn
  • 1,222
  • 1
  • 18
  • 41