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.
CodePudding user response:
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:
<layout>
<data>
...
</data>
<ConstraintLayout>
...
</ConstraintLayout>
</layout>
CodePudding user response:
Try something like this in your onCreate
function:
binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
And also add <layout> </layout>
tag in the XML.