android.view.WindowInsets ClassNotFoundException

后端 未结 2 1756
长发绾君心
长发绾君心 2021-01-05 20:42

android.view.WindowInsets is added in API level 20.

I import android.view.WindowInsets in my CustomLayout and override

相关标签:
2条回答
  • 2021-01-05 21:12

    What happens

    The system traverses all public methods of a view and encounters the overridden onApplyWindowInsets with WindowInsets parameter. This type does not exist in the system hence the crash.

    Lollipop introduced the View.onApplyWindowInsets method but it also introduced the OnApplyWindowInsetsListener, which if set, is invoked instead of the aforementioned method.

    When does it happen

    I've had reports of this on Samsung devices running Android 4.4.

    It can be triggered by dumping view hierarchy.

    What to do about it

    So far this doesn't solve anything. To the rescue comes support-v4 library:

    public class SampleView extends View {
        public SampleView(final Context context) {
            this(context, null);
        }
    
        public SampleView(final Context context, @Nullable final AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public SampleView(final Context context, @Nullable final AttributeSet attrs, final int defStyleAttr) {
            super(context, attrs, defStyleAttr);
    
            ViewCompat.setOnApplyWindowInsetsListener(this, new android.support.v4.view.OnApplyWindowInsetsListener() {
                @Override
                public WindowInsetsCompat onApplyWindowInsets(final View v, final WindowInsetsCompat insets) {
                    // Do whatever you needed to do in the first place...
                    return insets.consumeSystemWindowInsets();
                }
            });
        }
    }
    

    Use the above in your common constructor. WindowInsetsCompat is provided by the support-v4 library so it's always present, it does not expose any non-existent future classes directly on the view, and the code is effective only since Lollipop (where actual WindowInsets were introduced).

    Why is this happening

    Beats me.

    0 讨论(0)
  • 2021-01-05 21:29

    You cannot use android.view.WindowInsets in lower API versions if it was added in API level 20.

    0 讨论(0)
提交回复
热议问题