title: How to use View Binding with Included Views in Java?
body: <p>View Binding got released with v3.6.</p>
  <p><a href="https://developer.android.com/topic/libraries/view-binding" rel="nofollow noreferrer">https://developer.android.com/topic/libraries/view-binding</a></p>
  <p>I am going to use view binding with included layouts.</p>
  <p>So I refer to the following answer that is useful.</p>
  <p><a href="https://stackoverflow.com/questions/60405044/how-to-use-view-binding-with-included-views">How to use View Binding with Included Views?</a></p>
  <p>But it doesn't work well.</p>
  <blockquote>
  <h2>MainActivity.java</h2>
  </blockquote>
  <pre><code>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(&quot;this is a test&quot;);
      }
  }
  </code></pre>
  <blockquote>
  <h2>activity_main.xml</h2>
  </blockquote>
  <pre><code>&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:orientation=&quot;vertical&quot;&gt;
  
      &lt;include
          android:id=&quot;@+id/my_header&quot;
          layout=&quot;@layout/item_header&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;100dp&quot; /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  <p>However, the following error occurs.</p>
  <pre><code>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) 
  </code></pre>
  <p>What's wrong?</p>
  <p>If I remove id of include layout, there are other errors.</p>