I am trying to get the DisplayCutout and getting a
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.DisplayCutout android.view.WindowInsets.getDisplayCutout()' on a null object reference
Here is my code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
DisplayCutout displayCutout;
displayCutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
//Logger.e(TAG, "MARGIN " + displayCutout.getSafeInsetTop());
}
getRootWindowInsets returns null if and only if the view is detached. Make sure you're calling it from the correct context.
you should put your code on
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
DisplayCutout displayCutout;
displayCutout =
getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
//Logger.e(TAG, "MARGIN " + displayCutout.getSafeInsetTop());
}
}
I had to use a combination of OnApplyWindowInsetsListener
and get DisplayCutout
from DecorView
:
public class MyActivity extends AppCompatActivity implements androidx.core.view.OnApplyWindowInsetsListener {
private Rect insets = new Rect();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
//...
ViewCompat.setOnApplyWindowInsetsListener(getWindow().getDecorView(), this);
}
@Override
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
DisplayCutoutCompat cutoutCompat = insets.getDisplayCutout();
if (cutoutCompat != null) {
this.insets.set(cutoutCompat.getSafeInsetLeft(), cutoutCompat.getSafeInsetTop(), cutoutCompat.getSafeInsetRight(), cutoutCompat.getSafeInsetBottom());
} else {
this.insets.set(insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetTop(), insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom());
}
//cutoutCompat is null at this point... So get it the other way.
if (getWindow().getDecorView() != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowInsets rootWindowInsets = getWindow().getDecorView().getRootWindowInsets();
if (rootWindowInsets != null) {
DisplayCutout displayCutout = rootWindowInsets.getDisplayCutout();
if (displayCutout != null) {
this.insets.set(displayCutout.getSafeInsetLeft(), displayCutout.getSafeInsetTop(), displayCutout.getSafeInsetRight(), displayCutout.getSafeInsetBottom());
}
}
}
return insets;
}
}
public static boolean hasNotchInScreenOfAndroidP(View context) {
final boolean[] ret = {false};
final View view=context;
if (Build.VERSION.SDK_INT >= 28) {
if (context==null){
}else {
context.post(new Runnable() {
@Override
public void run() {
WindowInsets windowInsets=view.getRootWindowInsets();
if (windowInsets==null){
}else {
DisplayCutout displayCutout = view.getRootWindowInsets().getDisplayCutout();
if (displayCutout == null ) {
ret[0] = false;
} else {
List<Rect> rects = displayCutout.getBoundingRects();
if (rects == null || rects.size() == 0) {
ret[0] = false;
} else {
ret[0] = true;
}
}
}
}
});
}
}
return ret[0];
}
ComUtil.getStateBarHeightOfSeparationFromTheTop(this, getWindow().getDecorView());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With