Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intermittent NullPointerException from AppCompatDelegateImplV7.createSubDecor()

I have been seeing the crash below (via Crashlytics), but have not been able to identify the cause or reproduce the crash. It occurs on a wide variety of devices and Android versions. The app uses appcompat-v7:23.2.1. Is anyone else seeing it?

As you can see, the crash occurs within the onCreate() method of HomeActivity, which extends android.support.v7.app.AppCompatActivity. Within AppCompatDelegateImplV7.createSubDecor, the call to mWindow.findViewById(android.R.id.content) sometimes returns null. This in turn results in a NullPointerException on line 475. To me, this smacks of a race condition within the appcompat code.

The same crash occurs in another Activity, and both use CoordinatorLayout as their root layout element. This element was introduced at around the time the crash started showing up, so I can't help but wonder whether there's a connection.

Here is the relevant portion of the stack:

Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.ViewGroup.getChildCount()' on a null object reference
   at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:475)
   at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:309)
   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:273)
   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
   at com.bleacherreport.android.teamstream.activities.HomeActivity.onCreate(HomeActivity.java:181)
like image 223
Mark McClelland Avatar asked Apr 16 '16 03:04

Mark McClelland


1 Answers

Chris Banes proposed a workaround: within the Activity's onCreate() method, add a call to getWindow().getDecorView() prior to calling super.onCreate(). Initial testing of this workaround is very promising.

The code ends up looking like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    // Workaround for issue reported to Google: https://code.google.com/p/android/issues/detail?id=207638
    // Making this call here causes the content view to be populated, avoiding the occasional crashes due
    // to null content view when calling setContentView() below.
    getWindow().getDecorView();

    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_activity); // <-- Your Activity layout here

    ...
}

Update: with this workaround deployed, we are now seeing zero occurrences of this crash.

Update: This bug was reported fixed in 23.4.0.

like image 101
Mark McClelland Avatar answered Oct 10 '22 21:10

Mark McClelland