14

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)
Sagar
  • 23,903
  • 4
  • 62
  • 62
Farrux Bahodirov
  • 358
  • 4
  • 12

1 Answers1

17

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...

Marlon
  • 1,839
  • 2
  • 19
  • 42
pelotasplus
  • 9,852
  • 1
  • 35
  • 37
  • 1
    Thank you for the answer. I get the same crash on Android 8 occasionally. Hope this will solve the problem. In case anyone needs: binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_foo, null, false); – Hong Aug 14 '18 at 20:50
  • 4
    For info, this was reported to google: https://issuetracker.google.com/issues/71885063 . It was closed as "won't fix / not reproducible". Seems like devs reporting this are seeing it in crash reports (crashlytics, play store console) but have a difficult time reproducing it. In our app, it's our top crash report. – Carmen Nov 26 '18 at 11:50
  • 1
    Does anybody have an idea what could cause `android.R.id.content` to be absent? After a few hours of looking through AOSP source code to get ideas on how to reproduce this, I still haven't been able to reproduce it. However, it's our top reported crash, so many users are seeing it. – Carmen Nov 30 '18 at 14:39