2012-12-14 34 views
5

Tôi đã tạo bàn phím tùy chỉnh cho ứng dụng của mình. Bạn có thể xem mã cho bàn phím của tôi bên dưới. Tôi gặp lỗi trong trình xem xml Câu hỏi tương tự đã được hỏi Android - Unsupported Service: audio Nhưng không ai trả lời.Bàn phím tùy chỉnh Android, Không thể khởi tạo các lớp sau:

Mã cho Tuỳ chỉnh bàn phím của tôi

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

Lỗi Trong xml đồ họa 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) 

Xin hãy giúp tôi ra với điều này.

+0

Bạn có thể hiển thị mã 'KeyboardView' không? Một nơi nào đó bạn đang gọi 'getSystemService()'. – Eric

+0

KeyboardView là lớp android mà tôi chưa từng chạm vào. Ngoài ra tôi đã không gọi chức năng này. Tôi đã nhận được các lỗi tương tự trong văn bản chỉnh sửa tùy chỉnh của tôi nhưng chúng đã được giải quyết bằng cách sử dụng hàm isInEdit() –

+0

Tôi đã hỏi câu hỏi khác mà bạn đã đề cập đến. Tôi không bao giờ tìm thấy một giải pháp, phần lớn bởi vì nó trở nên không liên quan đến tôi - thay thế bàn phím tùy chỉnh bằng một dịch vụ trợ năng tốt hơn cho những gì chúng tôi đang làm. Không kém, bàn phím tùy chỉnh của chúng tôi hoạt động bất chấp lỗi. – Ian

Trả lời

2

Xem câu hỏi này và câu trả lời: Android - Unsupported Service: audio

Nói tóm lại: Đó là lỗi của nguồn Android. Nhưng tôi đã tìm ra giải pháp khắc phục nó.

Sử dụng KeyboardViewFix như thay 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); 
     }  
    } 
} 

Một lưu ý: Trên bắt đầu ứng dụng của bạn, trước khi bất kỳ mã khác mà bạn cần thiết lập KeyboardViewFix.inEditMode = false; hoặc bạn có thể nhận được một số sai sót.

+0

Không bao giờ thêm câu trả lời chỉ liên kết ... luôn tự viết xuống câu trả lời ... bạn luôn có thể sao chép/dán rằng, ít nhất bạn có thể đảm bảo rằng các liên kết sẽ không thay đổi, hoặc thối – Bonatti

+0

Trong câu hỏi khác, tôi cảnh báo về việc sao chép/dán câu trả lời tương tự Okey tôi sao chép câu trả lời ở đây. – Enyby

Các vấn đề liên quan