8
votes

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)
1
Crash also occurs with the following versions of appcompat-v7: 23.1.1; 23.2.0; 23.3.0 - Mark McClelland
CoordinatorLayout does not appear to be related to the crash; eliminating that did not make the crash go away. Oddly, downgrading Play Services from 8.4 to 7.8 does make the crash go away. Since I can't imagine how there could be any direct connection, I have to presume that Play Services 8.4 causes a timing difference that increases the likelihood of triggering a race condition. - Mark McClelland
Submitted issue report to Google: code.google.com/p/android/issues/… - Mark McClelland
Downgrading Play Services from 8.4 to 7.8 does not eliminate the crash; it merely reduces the frequency. - Mark McClelland
Chris Banes recommended a workaround (see answer below), claimed the issue (code.google.com/p/android/issues/…), and marked it to be addressed in a future release. - Mark McClelland

1 Answers

1
votes

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.