NULL Window Insets
Asked Answered
C

7

9

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());
}
Callery answered 1/12, 2018 at 21:8 Comment(1)
I experience the same problem, the best workaround I could find so far is to delay calling this method, for example doing it in a button click event.Zeidman
S
5

getRootWindowInsets returns null if and only if the view is detached. Make sure you're calling it from the correct context.

Salsala answered 2/12, 2018 at 0:53 Comment(1)
I'm calling it in onResume() and setContentView is called in onCreate() so there should be no issue with the activity being attached.Callery
B
4

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());
    }
}
Barrera answered 5/6, 2019 at 15:22 Comment(2)
but there is a bug #65621459Monosyllabic
App crashes on Android 7. It is still nullImpetus
T
2

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;
    }
}
Tbar answered 3/12, 2019 at 8:22 Comment(0)
B
1
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());

Brassware answered 2/8, 2019 at 9:51 Comment(0)
M
0

try below code. I was able to get status bar height with View.OnAttachStateChangeListener. In my case, I have attached listener to ScrollView(mDetailScrollView), change it to any view that you want to attach the listener.

    ...
    mDetailScrollView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
        @Override
        public void onViewAttachedToWindow(View v) {                
            DisplayCutout displayCutout = getDisplayCutout();
            if (displayCutout != null) {
                // your code...
            }
        }

        @Override
        public void onViewDetachedFromWindow(View v) {
        }
    });
    ...

    private DisplayCutout getDisplayCutout() {
        if (activity != null) {
            WindowInsets windowInsets = getWindow().getDecorView().getRootWindowInsets();
            if (windowInsets != null) {
                return windowInsets.getDisplayCutout();
            }
        }

        return null;
    }
Mytilene answered 14/2, 2019 at 16:48 Comment(0)
S
0

you can get DisplayCutout in handler

    val cutoutHandler = HandlerThread("cutout-thread")
    cutoutHandler.start()
    var handler = object : Handler(cutoutHandler.looper) {
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            runOnUiThread {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { // 9.0原生
                    val windowInsets = window.decorView.rootWindowInsets
                    val displayCutout = windowInsets.displayCutout
                }
                cutoutHandler.quit()
            }
        }
    }
    Thread {
        handler.sendEmptyMessage(0)
    }.start()
Syllabogram answered 12/4, 2019 at 2:52 Comment(0)
S
0

This was occurring for me when running an emulator. It looks like even if you have an emulator with a default skin that has a display cutout, it still may not register it as a display cutout device.

You can work around this by enabling the "Simulate a display with a cutout" in the developer options:
https://developer.android.com/guide/topics/display-cutout/#test_how_your_content_renders

Septime answered 17/7, 2019 at 18:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.