膨胀布局时的java.lang.ClassCastException

膨胀布局时的java.lang.ClassCastException,java,android,android-softkeyboard,Java,Android,Android Softkeyboard,我正在开发Android软键盘,并为按键应用自定义主题 我使用以下代码执行此操作: @Override public View onCreateInputView() { // Set custom theme to input view. int themeLayout = sharedPreferences.getInt(THEME_KEY, R.layout.input_1); mInputView = (LatinKeyboardView) getLayoutI

我正在开发Android软键盘,并为按键应用自定义主题

我使用以下代码执行此操作:

@Override
public View onCreateInputView() {

    // Set custom theme to input view.
    int themeLayout = sharedPreferences.getInt(THEME_KEY, R.layout.input_1);
    mInputView = (LatinKeyboardView) getLayoutInflater().inflate(
            themeLayout, null);
    mInputView.setOnKeyboardActionListener(this);

    // Apply the selected keyboard to the input view.
    setLatinKeyboard(getSelectedSubtype());

    return mInputView;
} 
但我得到了以下错误:

java.lang.ClassCastException:at com.xxx.xxx.android.SoftKeyboard.onCreateInputView (SoftKeyboard.java:159)在 com.xxx.xxx.android.SoftKeyboard.onStartInput (SoftKeyboard.java:232)在 android.inputmethodservice.inputmethodservice.doStartInput (InputMethodService.java:2641)位于 android.inputmethodservice.inputmethodservice$InputMethodImpl.startInput (InputMethodService.java:590)位于 android.inputmethodservice.IIInputMethodWrapper.executeMessage (IInputMethodWrapper.java:186)在 com.android.internal.os.HandlerCaller$MyHandler.handleMessage (HandlerCaller.java:37)位于android.os.Handler.dispatchMessage (Handler.java:102)位于 android.app.ActivityThread.main(ActivityThread.java:6682)位于 java.lang.reflect.Method.invoke(本机方法)位于 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1520)位于com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1410)

布局:

<com.sunzala.xxxx.android.LatinKeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@drawable/kb_bg_1"
    android:keyBackground="@drawable/key_bg_fill_grey"
    android:keyPreviewLayout="@layout/key_preview_layout"
    android:keyPreviewOffset="@dimen/keyPreviewOffset"
    android:keyTextColor="@color/white"
    android:popupLayout="@layout/keyboard_popup_layout" />


当我在我的设备上进行测试,但在发布应用程序后在崩溃日志中出现错误时,此功能正常。

可能proguard缩小了您的自定义视图。您可以通过在build.gradle文件的发布块中设置minifyEnabled false来禁用proguard

如果要使用proguard,请在proguard-rules.pro中添加以下行以保留自定义视图

-keep public class * extends android.view.View {
    public <init> (android.content.Context );
    public <init> (android.content.Context , android.util.AttributeSet );
    public <init> (android.content.Context , android.util.AttributeSet , int);
    public void set *(...);
}
-keep public class*扩展了android.view.view{
公共(android.content.Context);
public(android.content.Context,android.util.AttributeSet);
public(android.content.Context,android.util.AttributeSet,int);
公共无效集*(…);
}

gradle中未启用缩小的proguard。