5

我为我的应用程序创建了一个自定义键盘。您可以在下面看到我的键盘的代码。我在 xml 查看器中遇到错误 Android - Unsupported Service: audio 但没有人回答类似的问题。

我的自定义键盘的代码

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();
    }

xml 图形查看器中的错误

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)

这个你能帮我吗。

4

1 回答 1

2

请参阅此问题和答案:Android - 不支持的服务:音频

简而言之:这是android源代码的错误。但我找到了解决方案如何解决它。

用作KeyboardViewFix替换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);
        }       
    }
}

注意:在启动您的应用程序时,在您需要设置任何其他代码之前,KeyboardViewFix.inEditMode = false;否则您可能会遇到一些错误。

于 2015-11-28T21:56:21.710 回答