5

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 - Unsupported Service: audio But no one replied.

Code for my Custom keyboard

public class KeyboardViewExtend extends KeyboardView implements KeyboardView.OnKeyboardActionListener
    {

    private SymjaBase _parent = null;
    private Keyboard _myKeyboard = null;


    public KeyboardViewExtend(Context c, AttributeSet atts, int defStyle) {
        super(c, atts, defStyle);
        // Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse
        if (!isInEditMode()) {
            _parent = (SymjaBase)c; 
        }
        init(c);
    }

    public KeyboardViewExtend(Context c, AttributeSet atts) {
        super(c, atts);
        // Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse
        if (!isInEditMode()) {
            _parent = (SymjaBase)c; 
        }
        init(c);
    }


    private void init(Context c) {
        setOnKeyboardActionListener(this);
        setEnabled(true);  
        setPreviewEnabled(true);   
        setFocusable(false);
        setFocusableInTouchMode(false);
        setBackgroundColor( Color.BLACK );
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0);
        lp.gravity = Gravity.BOTTOM;
        setLayoutParams(lp);
        makeKeyboardView();
    }

Error In xml Graphical Viewer

The following classes could not be instantiated:
- org.matheclipse.android.KeyboardViewExtend (Open Class, Show Error Log)
See the Error Log (Window > Show View) for more details.
Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse

java.lang.UnsupportedOperationException: Unsupported Service: audio
    at com.android.layoutlib.bridge.android.BridgeContext.getSystemService(BridgeContext.java:448)
    at android.inputmethodservice.KeyboardView.<init>(KeyboardView.java:376)
    at android.inputmethodservice.KeyboardView.<init>(KeyboardView.java:279)
    at org.matheclipse.android.KeyboardViewExtend.<init>(KeyboardViewExtend.java:35)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(    at sun.reflect.NativeConstructorAccessorImpl.newInstance(    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(    at java.lang.reflect.Constructor.newInstance(    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:413)
    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:170)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:135)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:746)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:372)

Please help me out with this.

Community
  • 1
  • 1
Shakti Malik
  • 2,335
  • 25
  • 32
  • Can you show the `KeyboardView` code? Somewhere you're calling `getSystemService()`. – Cat Dec 14 '12 at 19:12
  • KeyboardView is android class which i haven't touched. Also I haven't called this function. I was getting Similar errors in My custom edit text but they got resolved by using isInEdit() function – Shakti Malik Dec 14 '12 at 19:14
  • I asked the other question you referred to. I never found a solution, largely because it became irrelevant to me - replaces the custom keyboard with an accessibility service which was better for what we were doing. None the less, our custom keyboard worked despite the error. – Ian Dec 16 '12 at 08:36
  • Yes , Mine custom keyboard also works fine. But xml graphics layout is not able to show keyboard which kind of make it hard to visualize. I had same issue in my custom editText, which got solved by using "isInEditMode()". But keyboard thing is still not fixed. I will be glad if someone can help – Shakti Malik Dec 16 '12 at 19:46
  • 1
    Did you get it working ? Even I am facing the same problem. :( – Swayam Feb 05 '13 at 19:34
  • @Swayam I have tried it all but still no success. Please share if you are able to work it out – Shakti Malik Jun 19 '14 at 11:54

1 Answers1

2

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.

Community
  • 1
  • 1
Enyby
  • 4,162
  • 2
  • 33
  • 42
  • Never add answers that are link only... always writte down the answer yourself... you can always copy/paste that, at least you can guarantee that the link wont change, or rot – Bonatti Mar 23 '16 at 14:08
  • In other question me warned aboutcopy/paste same answer. Okey I copied answer here. – Enyby Mar 23 '16 at 20:57