Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App crashing when using android data-binding on android 8

The code work on pre-oreo devices, but Crashlytics saying it crashing on android 8 devices

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getAppComponent().inject(this);
        binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
    }

The stacktrace

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.ViewGroup.getChildCount()' on a null object reference

Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.ViewGroup.getChildCount()' on a null object reference
       at android.databinding.DataBindingUtil.bindToAddedViews(DataBindingUtil.java:295)
       at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:279)
       at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:261)
       at com.myapp.MyActivity.onCreate(MyActivity.java:59)
       at android.app.Activity.performCreate(Activity.java:7174)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
       at android.app.ActivityThread.-wrap11(Unknown Source)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
       at android.os.Handler.dispatchMessage(Handler.java:105)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6940)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
like image 776
Farrux Bahodirov Avatar asked Apr 27 '18 10:04

Farrux Bahodirov


People also ask

Is Data Binding good in Android?

Using data binding can lead to faster development times, faster execution times and more readable and maintained code. Android data binding generates binding classes at compile time for layouts.

What causes app crashes on Android?

This usually occurs when your Wi-Fi or cellular data is slow or unstable, causing apps to malfunction. Another reason for Android apps crashing can be a lack of storage space in your device. This can occur when you overload your device's internal memory with heavy apps.


1 Answers

Have similar crashes with exactly the same stacktrace on 8.

Have you tried using:

binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity, null, false);
setContentView(binding.getRoot());

Difference between code above and calling

DataBindingUtil.setContentView(...);

is that inflate() returns a View directly which is later passed to DataBindingUtils::bindToAddedViews. In case of DataBindingUtil.setContentView the following logic is being used

activity.setContentView(layoutId);
View decorView = activity.getWindow().getDecorView();
ViewGroup contentView = (ViewGroup) decorView.findViewById(android.R.id.content);

and it seems that

ViewGroup contentView = (ViewGroup) decorView.findViewById(android.R.id.content);

is just NULL...

like image 105
pelotasplus Avatar answered Sep 21 '22 21:09

pelotasplus