'Data binding: Missing required view with ID

I create an app using binding and I get an error that I couldn't solve. The error is

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newtest/com.example.mylibrary.MainActivity1}: java.lang.NullPointerException: Missing required view with ID: com.example.newtest:id/myRecyclerview

Thanks!

public class MainActivity1 extends AppCompatActivity {

    RecyclerView.LayoutManager layoutManager;
    ArrayList<POJOimage>pojoImageArrayList;
    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        layoutManager = new LinearLayoutManager(this);
        binding.myRecyclerview.setLayoutManager(layoutManager);
        init();
    }

XML:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity1"
    android:background="#000"
    android:padding="10dp">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/myRecyclerview"/>
</androidx.constraintlayout.widget.ConstraintLayout>


Solution 1:[1]

I strongly suspect some Android Studio bug.

I had the same bug, and everything I've had done was adding some TextView. As it was easy to find this difference, I've just started fiddling with this TextView in xml. At some point bug disappeared. And here comes the best. Before starting to deal with the problem I've saved problematic change with git. So at the end I've reapplied this change and ... everything worked. No more bug !

P.S. As this bug reappears, I've found that the easiest solution for getting rid of it is to use "Clean Project" from "Build" menu.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1