I am using dataBinding
I have MainActivity
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
}}
My gradle file
buildFeatures {
viewBinding true
dataBinding true
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.4"}
I am getting runtime error
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘android.view.View com.actofit.beat.databinding.ActivityMainBinding.getRoot()’ on a null object reference
I can’t figure out what is the null object reference, that’s what I need help for
2
Answers
It seems like a problem with your XML structure. Did you check that the root element is a view group (like Relative layout, Constraint layout)?
It should look similar to:
Try something like this in your
onCreate
function:And also add
<layout> </layout>
tag in the XML.