Android Custom keyboard , The following classes could not be instantiated:

后端 未结 1 1198
陌清茗
陌清茗 2021-01-13 01:28

I have created a custom keyboard for my app. You can see the code for my keyboard below. I am getting error in xml viewer Similar question has been asked Android - Unsuppo

相关标签:
1条回答
  • 2021-01-13 01:51

    See this question and answers: Android - Unsupported Service: audio

    In short: It is bug of android source. But I found solution how fix it.

    Use KeyboardViewFix as replace KeyboardView:

    public class KeyboardViewFix extends KeyboardView {
        public static boolean inEditMode = true;
    
        @TargetApi(21) // Build.VERSION_CODES.L
        public KeyboardViewFix(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(inEditMode ? new ContextWrapperInner(context) : context, attrs, defStyleAttr, defStyleRes);
        }
    
        public KeyboardViewFix(Context context, AttributeSet attrs, int defStyleAttr) {
            super(inEditMode ? new ContextWrapperInner(context) : context, attrs, defStyleAttr);
        }
    
        public KeyboardViewFix(Context context, AttributeSet attrs) {
            super(inEditMode ? new ContextWrapperInner(context) : context, attrs);
        }
    
        public static class ContextWrapperInner extends ContextWrapper {
            Context base;
            public ContextWrapperInner(Context base) {
                super(base);
                this.base = base;
            }
            public Object getSystemService(String name) {
                return Context.AUDIO_SERVICE.equals(name) ? null : base.getSystemService(name);
            }       
        }
    }
    

    One note: On start your app, before any other code you need set KeyboardViewFix.inEditMode = false; or you can get some errors.

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